How to find that has a string and pull the record out of that file

Hello Experts !!!

Have some trouble finding a solution for the problem mentioned below.

Please help.
Thanks,
Lee.

I have set of input files as below

File1

MCMCNDD77  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A
HHFDKKF  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A
FKJDJDKJ  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A

File2

MCMCNDD77  20100903:12:36:50        234        AC,MRP,NWER    CKDJKJK838838               GDDK-DKCJJ                              A
MCMCNDD77  20100903:12:36:50        323-2322        AC,MRP,NWER    CKDJKJK838838               234                              A
MCMCNDD77  20100903:12:36:50        234        CMC,MRP,NWER    CKDJKJK838838               GDDK-DKCJJ                              A

File3

MCMCNDD77  20100903:12:36:50        234        BAC,MRP,NWER    CKDJKJK838838               FHHF-DCCJJ                              A
MCMCNDD77  20100903:12:36:50        234        BAC,MRP,NWER    CKDJKJK838838               GDDK-DKCJJ                              A
MCMCNDD77  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               GDDK-DKCJJ                              A

I wish to search for all files in the directory that have the number "234" in the sixth place as below

$cat file1 | awk '{print $6}'

and then YANK that record out and place in lets say an Outputfile.

So, in the above scenario with the files, file1, file2, file3, I want to see the four records out in Outputfile as below.

MCMCNDD77  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A
HHFDKKF  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A
FKJDJDKJ  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A
MCMCNDD77  20100903:12:36:50        323-2322        AC,MRP,NWER    CKDJKJK838838               234                              A
awk '$6 == 234' File[123] > OutputFile

Scottn,

I was thinking this has to do with awk & there you were !!! Im so glad you helped me.

Just want to tag another request to this one...if I want to replace that 234 with MCCDK in the sixth column inside that file, how do I do this ?

Thanks,
Lee.

Hi.

awk '$6 == 234 { sub($6, "MCCDK"); print }' File[123] > OutputFile

(you could also use { $6 = "MCCDK"; print } but you'd lose formatting)

1 Like

Scottn,

Im so so thankful for your help. Im glad I joined here.

Lee.