Monday, July 14, 2008

Merging lines using awk - bash


Input file:

$ cat myfile.txt

unix,bash
,scripting
awk is powerful
u1,u2
,u3,u4,u5


Objective: Merge the lines starting with comma "," with the previous line.
i.e. the output required.

unix,bash,scripting
awk is powerful
u1,u2,u3,u4,u5


Awk solution:

$ awk 'END { print r }
r && !/^,/ { print r; r = "" }
{ r = r ? r $0 : $0 }
' myfile.txt

1 comment:

awkseeker said...

Could you explain the code?

© Jadu Saikia www.UNIXCL.com