zoukankan      html  css  js  c++  java
  • Linux/shell: Concatenate multiple lines to one line

    $ cat file
    START
    Unix
    Linux
    START
    Solaris
    Aix
    SCO

    1. Join the lines following the pattern START without any delimiter.
    $ awk '/START/{if (NR!=1)print "";next}{printf $0}END{print "";}' file
    UnixLinux
    SolarisAixSCO
    

    2. Join the lines following the pattern START with space as delimiter.
    $ awk '/START/{if (NR!=1)print "";next}{printf "%s ",$0}END{print "";}' file
    Unix Linux
    Solaris Aix SCO
    

    3. Join the lines following the pattern START with comma as delimiter.
    $ awk '/START/{if (x)print x;x="";next}{x=(!x)?$0:x","$0;}END{print x;}' file
    Unix,Linux
    Solaris,Aix,SCO
    
    4. Join the lines following the pattern START with comma as delimiter with also the pattern matching line.
    $ awk '/START/{if (x)print x;x="";}{x=(!x)?$0:x","$0;}END{print x;}' file
    START,Unix,Linux
    START,Solaris,Aix,SCO
    

    5. Join the lines following the pattern START with comma as delimiter with also the pattern matching line. However, the pattern line should not be joined.
    $ awk '/START/{if (x)print x;print;x="";next}{x=(!x)?$0:x","$0;}END{print x;}' file
    START
    Unix,Linux
    START
    Solaris,Aix,SCO

    6. Other ways:
    paste -d, -s file

    cat file | xargs


    REF:

    http://www.theunixschool.com/2012/05/awk-join-or-merge-lines-on-finding.html

    https://stackoverflow.com/questions/15758814/turning-multiple-lines-into-one-line-with-comma-separated-perl-sed-awk

    ==========================================================

    AC  M07451
    ID  V$PBX1_10
    NA  PBX1
    AC  M07452
    ID  V$MEIS1_06
    NA  Meis1
    AC  M07453
    ID  V$MEIS1BHOXA9PBX1A_01
    NA  Meis1b:Hoxa9:Pbx1a
    AC  M07454
    ID  V$GLI1_Q3_01
    NA  Gli1
    AC  M07455
    ID  V$GLI2_Q3
    NA  Gli2
    AC  M07456
    ID  V$HOXA913_Q4
    NA  HOX 9-13

    awk '$0 ~ /^AC|^ID|^NA/'  conserved.new

     |  awk '/AC  /{if (x)print x;x="";}{x=(!x)?$0:x","$0;}END{print x;}'

        | sed 's/AC  //g' | sed 's/,ID  / /g' | sed 's/,NA  / /g'

                  >  genesymbol


    M07451    V$PBX1_10    PBX1
    M07452    V$MEIS1_06    Meis1
    M07453    V$MEIS1BHOXA9PBX1A_01    Meis1b:Hoxa9:Pbx1a
    M07454    V$GLI1_Q3_01    Gli1
    M07455    V$GLI2_Q3    Gli2
    ==========================================================

    more file
    start111
    aa
    bb
    cc
    start222
    dd
    ee

    awk '/start/{if (x)print x;x="";}{x=(!x)?$0:x","$0;}END{print x;}' file > bbb

    more bbb
    start111,aa,bb,cc
    start222,dd,ee

    awk -F"[|, ]" '{for(i=2;i<=NF;i++){print $1"|"$i}}' bbb
    start111|aa
    start111|bb
    start111|cc
    start222|dd
    start222|ee

  • 相关阅读:
    English trip V2-B 20 Happy Holiday Teacher: Russell
    English Voice of <<That Girl>>
    English trip V2-B 19 How often is often? Teacher: GABRIELE
    English trip EM4-LP 3A AT The MARKET Teacher:Patrick
    I1-3 Telephone English Teacher:Taylor
    Phonics 自然拼读法 ar er ir ur or 元音字母组合 Teacher:Lamb
    English trip V2-B 18 What's Your Job? 你是什么工作 Teacher: Russell
    English trip V2-B 17 Look to the Future Teacher: Russell
    Huawei设备配置系统时间
    iperf 网络测试工具
  • 原文地址:https://www.cnblogs.com/emanlee/p/7983668.html
Copyright © 2011-2022 走看看