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  /\t/g' | sed 's/,NA  /\t/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

 

posted @ 2017-12-05 05:20 emanlee 阅读(...) 评论(...) 编辑 收藏