Friday, May 23, 2008

Concatenating multiple lines - awk


The input file:


$ cat details.txt
Name:Mr Abraham
Location:
India
Name:Mrs Jeen
Location:
Holland:4354
Name:Yenn
Location:
China:3454
Beijing


Objective:
- Concate the lines into single lines such that each line starts with the pattern "Name"
i.e. Required Output:


Name:Mr Abraham Location: India
Name:Mrs Jeen Location: Holland:4354
Name:Yenn Location: China:3454 Beijing


Solution:

$ awk 'NF&&$1=RS$1' RS="Name" details.txt


A non awk way of doing the same:

$ echo `tr "\n" " " < details.txt` | sed 's/Name/%%Name/g' | tr -s "%" "\n"

No comments:

© Jadu Saikia www.UNIXCL.com