Saturday, April 12, 2008

Combine every 3 lines as one - Awk


$ cat details.txt
AA XX
ID23145
Singapore,1982
BB YZY
SD1243
Delhi(India),1980
DD AS
ASD324
Dubai,1981

Purpose:
-------
Combine every 3 lines as one i.e.

Required Output
----------------
AA XX,ID23145,Singapore,1982
BB YZY,SD1243,Delhi(India),1980
DD AS,ASD324,Dubai,1981


If line number is divisible by 3 then put a new line(\n) else put a comma(,) i.e.

$ awk '{printf("%s%s", $0, (NR%3 ? "," : "\n"))}' details.txt

1 comment:

voyeg3r said...

paste -d"," - - - details.txt

© Jadu Saikia www.UNIXCL.com