LQ Newbie
Registered: Sep 2012
Location: North East USA
Distribution: at work: Red Hat Enterprise Linux Server release 5.8 (Tikanga); at home: what do you recommend?
Posts: 24
Rep:
|
Matching two fields between two files with awk
Hi,
I have two files I am trying to find the items on File2 based on Field3 and Field4 matching the content of File1 Field1 and Field2. I may have other fields to check, but I figured if I understood how to do two, I could go from there. I’m not really sure how to set this up. I do not have Perl, and yes, I am looking at awk specifically, I realize grep may be an option too, though my question is about awk. Appreciate the help, bop-a-nator
File1:
Fruit,Kiwi
Fruit,Pear
Veggie,carrot
Dairy,Milk
File2:
Item,A,Veggie,tomato,20120903,Bin,42
Item,B,Fruit,Kiwi,20120901,Bin,2
Item,B,Fruit,Grapes,20120903,Bin,12
Item,A,Dairy,Milk,20120921,Fridge,3
Item,B,Fruit,Pear,20120903,Bin,14
Item,C,Veggie,carrot,20120903,Bin,45
Item,B,Veggie,celery,20120903,Bin,32
Output I am trying to get:
Item,B,Fruit,Kiwi,20120901
Item,A,Dairy,Milk,20120921
Item,B,Fruit,Pear,20120903
Item,C,Veggie,carrot,20120903
What I ran:
/bin/gawk 'BEGIN{OFS=FS=","}NR==FNR{file1[$3,$4]=$0;next} $1,$2 in file1 {print $1,$2,$3,$4,$5}' File1 File2 > Fileout
What came out on Fileout:
Item,A,Veggie,tomato,20120903
Item,B,Fruit,Kiwi,20120901
Item,B,Fruit,Grapes,20120903
Item,A,Dairy,Milk,20120921
Item,B,Fruit,Pear,20120903
Item,C,Veggie,carrot,20120903
Item,B,Veggie,celery,20120903
|