parsingsedcentoscap

Join multiple lines into One (.cap file) CentOS


Single entry has multiple lines. Each entry is separated by two blank lines. Each entry has to be made into a single line followed by a delimiter(;).

Sample Input:

Name:Sid
ID:123


Name:Jai
ID:234


Name:Arun
ID:12

Tried replacing the blank lines with cat test.cap | tr -s [:space:] ';'

Output:

Name:Sid;ID:123;Name:Jai;ID:234;Name:Arun;ID:12;

Expected Output:

Name:SidID:123;Name:JaiID:234;Name:ArunID:12;

Same is the case with Xargs.

I've used sed command as well but it only joined two lines into one. Where as I've 132 lines as one entry and 1000 such entries in one file.


Solution

  • You may use

    cat file | awk  'BEGIN { FS = "\n"; RS = "\n\n"; ORS=";" } { gsub(/\n/, "", $0); print }' | sed 's/;;*$//' > output.file
    

    Output:

    Name:SidID:123;Name:JaiID:234;Name:ArunID:12
    

    Notes:

    See the online demo