Concatenating the lines of a data

I have a data of 1 lac lines with the following format

abcde,1,2,3,4,
,ee
,ff
,gg
,hh
,mm
abcde,3,4,5,6,
,we
,qw
,as
,zx
,cf
abcde,1,5,6,7,
,dd
,aa
,er
....
....
....
abcde,1,5,4,3,
,kk
,gh

abcde is there in all record , it is starting word and below lines are sub records

I would like to concatenate to make the output like

abcde,1,2,3,4,ee,ff,gg,hh,mm
abcde,3,4,5,6,we,qw,as,zx,cf
.
.
.
.
abcde,1,5,4,3,kk,gh

Please help with a script either in Unix Shell or Perl

Is the trailing comma on the "abcde" lines always present? It seems inconsistent with the structure of the rest of the data file.

Hi aravindj80,

One solution using perl:

$ cat infile
abcde,1,2,3,4,
,ee
,ff
,gg
,hh
,mm
abcde,3,4,5,6,
,we
,qw
,as
,zx
,cf
abcde,1,5,6,7,
,dd
,aa
,er
....
....
....
abcde,1,5,4,3,
,kk
,gh
$ perl -pe 'chomp; print qq[\n] if $. > 1 && m/\A(?i:abcde)/ ; print qq[\n] if eof' infile
abcde,1,2,3,4,,ee,ff,gg,hh,mm
abcde,3,4,5,6,,we,qw,as,zx,cf
abcde,1,5,6,7,,dd,aa,er............
abcde,1,5,4,3,,kk

Regards,
Birei

1 Like

awk solution

# awk '{a[x++]=$0}END{for(i=0;i<=x;i++)if(a!~/abcde/)printf "%s",a;else{if(i!=0)printf "\n%s",a;else{printf "%s",a}}}' file
abcde,1,2,3,4,,ee,ff,gg,hh,mm
abcde,3,4,5,6,,we,qw,as,zx,cf
abcde,1,5,6,7,,dd,aa,er............
abcde,1,5,4,3,,kk,gh

regards
ygemici

Hmm. I asked about the trailing comma because the sample output in post #1 does not contain a double-comma.

Nice touch, methyl. I read your previous message but didn't realize. I will change the script later because it seems wrong as is.

Regards,
Birei

Birei You are great and Superb , it worked very fast and can you help me for this also on perl