Issue while append to previous line

Hi, I have data as below.

36578019,005-923887317,UNMDL,20151230,2C3CCAAG4GH135448,L,TX,20160108,62,"030916 PPT TX AFF RPRT VALID AFF IN PDP WLL FWD TO
RYAN ON 031116 CB1619                             
",,
36580219,611-923785453,FC,20151209,ZACCJABT9FPC19274,L,TX,20160108,83,,,
36581327,611-924085106,UNMDL,20151230,1C6RR7MT8GS151541,L,TX,20160108,62,,,

First 3 lines are problematic records, they should come in single as below.

36578019,005-923887317,UNMDL,20151230,2C3CCAAG4GH135448,L,TX,20160108,62,"030916 PPT TX AFF RPRT VALID AFF IN PDP WLL FWD TO RYAN ON 031116 CB1619                             ",,
36580219,611-923785453,FC,20151209,ZACCJABT9FPC19274,L,TX,20160108,83,,,
36581327,611-924085106,UNMDL,20151230,1C6RR7MT8GS151541,L,TX,20160108,62,,,

I tried below command, my bad, not working, any help ??

awk 'NR==1{s=$0;next} /^["]|^;/{s=s$0;next} {print s;s=$0} END{if(s)print s}' PPT1.csv

Hello JSKOBS,

Could you please try following and let me know if this helps you.

awk '($0 ~ /^[[:digit:]]/ && A){print A;A=""} {gsub(/^[[:space:]]+|[[:space:]]+$/,X,$0);A=A?A OFS $0:$0} END{print A}'   Input_file

Output will be as follows.

36578019,005-923887317,UNMDL,20151230,2C3CCAAG4GH135448,L,TX,20160108,62,"030916 PPT TX AFF RPRT VALID AFF IN PDP WLL FWD TO RYAN ON 031116 CB1619 ",,
36580219,611-923785453,FC,20151209,ZACCJABT9FPC19274,L,TX,20160108,83,,,
36581327,611-924085106,UNMDL,20151230,1C6RR7MT8GS151541,L,TX,20160108,62,,,

Thanks,
R. Singh

1 Like

Thanks Ravinder for reply, working perfect.
Would you mind explaining me the command. For my knowledge, its difficult to understand

Hello JSKOBS,

Glad it worked for you. Could you please go through following and let me know if this helps you.

awk '($0 ~ /^[[:digit:]]/ && A){            #### Checking if the current line is starting with DIGITS and variable named A is NOT NULL, then perform following statements.
print A;                                    #### printing variable named A.
A=""}                                       #### Nullifying the value of variable A.
{gsub(/^[[:space:]]+|[[:space:]]+$/,X,$0);  #### I am substituting spaces from the starting of the lines and last of the lines here. where gsub means global substitution in awk.
A=A?A OFS $0:$0                             #### Creating variable named A whose value will be current line's value and it will keep concatenating it's own A's value till above 1st condition becomes TRUE where line starting from digits, there we are nullifying it's value.
} 
END                                         #### END section here.
{print A}                                   #### printing the value of A, because last line's A value should be printed here.
'   Input_file                              #### Mentioning Input_file here for awk to be read.
 

Thanks,
R. Singh

2 Likes

Try also (although, alas, untested and no error checking (e.g. EOF) done)

awk '{while (gsub(/"/, "&")%2) {getline T; $0 = $0 FS T}} 1' file
1 Like