Combining multiple block of lines in one comma separated line

Hi Everyone,

On my Linux box I have a text file having block of few lines and this block lines separated by one blank line. I would like to format and print these lines in such a way that this entire block of lines will come as single comma separated line & again next block of lines in next single line and so on.

Following is the pattern of data in text file,

Name:		abc
CName:		xyz
Three:		def
Event:		Off
Five:		server1
Six:		2014-06-04 03:02:05.353
Number:		123456789
Priority:	High
Nine:		9
Data:		abc def ghi jkl mno pqr stv
Count:		9

Name:		tmp
CName:		bar
Three:		foo
Event:		On
Five:		server2
Six:		2014-06-04 05:10:05.353
Number:		123456789
Priority:	Low
Nine:		9
Data:		abc def ghi jkl mno pqr stv
Count:		9

And after formatting/parsing output look like below,

2014-06-04 03:02:05.353,	Name:abc,CName:xyz,Three:def,Event:Off,Five:server1,Number:123456789,Priority:High,Data:abc def ghi jkl mno pqr stv,Count:9
2014-06-04 05:10:05.353,	Name:tmp,CName:bar,Three:foo,Event:On,Five:server2,Number:123456789,Priority:Low,Data:abc def ghi jkl mno pqr stv,Count:9

I really need your valuable inputs to get this ....

Thanks in advance.

An awk approach:

awk '
        NF {
                if ( $0 ~ /Six/ )
                {
                        H = $0
                        sub(/Six:[ \t]*/, X, H)
                }
                else
                {
                        gsub("\t", X, $0)
                        S = S ? S OFS $0 : $0
                }
                next
        }
        !NF {
                print H, S
                S = ""
        }
        END {
                print H, S
        }
' OFS=, file

Hi Yoda, thanks for your quick reply. I would like to know if if i can run this code as one liner ? If yes how to do that . Tks

Just put the code in one line!

awk 'NF{if($0~/Six/){H=$0;sub(/Six:[ \t]*/,X,H)}else{gsub("\t",X,$0);S=S?S OFS $0:$0}next}!NF{print H,S;S=""}END{print H,S}' OFS=, file
1 Like

Try

awk '{gsub("[\t ]*","");$1=$6"\t"$1; sub (/^[^:]*:/,"",$1); $6=""}1'  RS= ORS="\n" FS="\n" OFS="," file
1 Like

Hi Yoda & RudiC, thank you very much for help both the solutions worked for me.

Hello,

One more approach for same.

awk -F":" -vs1="," -vpun=",\t" '/Six:/ {s=1;match($0,/[0-9][0-9][0-9][0-9]\-.*/);val=substr($0,RSTART,RLENGTH)} !/Six:/ {s=0} !s{gsub(/^[[:space:]]+/,X,$2);a=a?a s1 $1 OFS $2:$1 OFS $2} /Count:/ {p=1} {if(p){gsub(/^\:\,/,X,a);print val pun a;s=0;p=0;a=""}}' OFS=":" filename

Output will be as follows.

2014-06-04 03:02:05.353,        Name:abc,CName:xyz,Three:def,Event:Off,Five:server1,Number:123456789,Priority:High,Nine:9,Data:abc def ghi jkl mno pqr stv,Count:9
2014-06-04 05:10:05.353,        Name:tmp,CName:bar,Three:foo,Event:On,Five:server2,Number:123456789,Priority:Low,Nine:9,Data:abc def ghi jkl mno pqr stv,Count:9

Thanks,
R. Singh

@RavinderSingh, thanks for your reply. I yet to try out the code. Will let you know as soon as I can.

Thanks again.