Concatenating the lines with different pattern
Hi,
I have put a similar question in one of the other threads through which I got the solution shown below but I have some more condition to add to it, hence have further queries on it. I appologies if I should be putting this with the old thread.
I have a file which perform a grep on the original file and put the result into another file File2.
grep -E "^SH1|^RD1" Original_File1 > File2 ( which has the data as shown below)
cat File2:
SH1,QC,12334
RD1,MO,898909,35476
SH1,BC,34556
RD1,FG,2341212,909090
SH1,TR,787878
RD1,GH,12345,676767
SH1,YO,565656
RD1,GO,7878604,23978
Then I combined the two records (SH1 AND RD1) using sed command and put it into File3.
sed 'N;s/\n/,/' File2 > File3
cat File3:
SH1,QC,12334,RD1,MO,898909,35476
SH1,BC,34556,RD1,FG,2341212,909090
SH1,TR,787878,RD1,GH,12345,676767
SH1,YO,565656,RD1,GO,7878604,23978
New Condition:
I am trying to do is: if the SH1 record is missing the corresponding RD1 record below it then append 8 (commas) after the end SH1 record else append the RD1 record(if existis). Also, vice versa...if RD1 record is missing the SH1 record above it, then prepend the record with 4 (commas).
All I trying to do is to create a file without combining SH1 record with SH1 record or RD1 record with RD1 record. Can we perform a loop to check ths.
If the record is something like this
SH1,QC,12334
RD1,MO,898909,35476 --- let say if this record is missing then in that case I want to add 6 commas in the above record.
SH1,BC,34556
RD1,FG,2341212,909090
SH1,TR,787878 ---let say this record is missing
RD1,GH,12345,676767
SH1,YO,565656
RD1,GO,7878604,23978
Output:
SH1,QC,12334,,,,,,,
SH1,BC,34556,RD1,FG,2341212,909090
,,,,RD1,GH,12345,676767
SH1,YO,565656,RD1,GO,7878604,23978
I will really appreciate your advice on this.
Thanks