Help in below scenario

Hi,
my file has the data like below:

11,231,ABCVAV
22,AAHJHAj22,hdsjkhdls
22,dhskjhdkshd
22,gdgkdkadh
11,232,dgsjgdjh
22,ghdskahdkja
22,shdkajshs
11,233,ddjs
22,dhjkahkd
22,hsajhaah
11,231,sjkjsjj
22,ahkh
22,hsakh

From the above i need only the records which starts as 11,231 and 11,232 and their corresponding records start with 22

11,231,ABCVAV
22,AAHJHAj
22,hdsjkhdls
22,dhskjhdkshd
22,gdgkdkadh
11,232,dgsjgdjh
22,ghdskahdkja
22,shdkajshs
11,231,sjkjsjj
22,ahkh
22,hsakh
 

How to achieve this in sed or awk?

Thanks

duplicate

---------- Post updated at 01:52 PM ---------- Previous update was at 01:29 PM ----------

$ nawk -F, '{if($0~/11,23[12]/){a=0;print $0}else if($0~/11,23[3-9]/){a=1}if(a==0 && $0~/^22/){print $0}} ' input.txt

How i can bring all starts with 11,231 and 11,232 i single line:

Like:

11,231,ABCVAV|22,AAHJHAj|22,hdsjkhdls|22,dhskjhdkshd|22,gdgkdkadh
11,232,dgsjgdjh|22,ghdskahdkja|22,shdkajshs
11,231,sjkjsjj|22,ahkh|22,hsakh
$ nawk -F, '{if($0~/11,23[12]/){a=0;printf("\n%s|",$0)}else if($0~/11,23[3-9]/){a=1;printf("\n")}if(a==0 && $0~/^22/){printf("%s|",$0)}} ' input.txt

remove the empty linse manually or use sed or grep

$ nawk -F, '{if($0~/11,23[12]/){a=0;printf("\n%s|",$0)}else if($0~/11,23[3-9]/){a=1;printf("\n")}if(a==0 && $0~/^22/){printf("%s|",$0)}} ' input.txt | grep  .

Thanks for your help:
i need to extract the below record from my input:

88,. ABVC IK:000008 GMT DET:ACFJ

I need to match the pattern GMT DET and i need to fetch those records.
I have executed the below command:

nawk -F, '{if($0~/^88,*GMT*DET*/) {print $0}}' input.txt

But it returns null.
Am i doing anything wrong?

Thanks

sed -n '/^88,.*GMT.*DET/p' myFile
nawk '/^88,.*GMT.*DET/' myFile
1 Like