Hello All,
I am having a below requirement.
File1 contains
KEY|VIN|SEQUENCE|COST
101 | XXX111 | 1 | 234.22
234 | XXX111 | 2 | 134.32
444 | ABC234 | 1 | 100.22
555 | DFF611 | 1 | 734.82
FILE 2 Contains only VINs
XXX111
DFF611
Now if the VIN from file 1 is present in File 2 I want to write the whole row for the VIN in a seperate file. Please help.
Desired Output
101 | XXX111 | 1 | 234.22
234 | XXX111 | 2 | 134.32
555 | DFF611 | 1 | 734.82
Please help me with an awk script which can do this task faster. Its really urgent for me
awk 'NR==FNR{++a[$0]}a[$3]' file2 file1
1 Like
Try this
awk -F"|" 'NR==FNR{a[$0]}{if($2 in a){print}} file2 file1
regards,
Ahamed
1 Like
Hi Ahmed,
It works ...Thanks a lot...
I want to do this processing for millions of records. Hope it completes quickly...
Thanks for your timely help..
---------- Post updated at 11:35 PM ---------- Previous update was at 11:34 PM ----------
Hello Danmero..
Thanks a lot for your reply...I tried it..But it is not working..Below code is working for me
---------- Post updated 05-18-11 at 12:16 AM ---------- Previous update was 05-17-11 at 11:35 PM ----------
Thanks a lot Ahmed..u made my day....!!! it processed millions of records in minutes :)
Datasample posted on your OP is not what you have.
My solution works on your original datasample and ahamed101's solution don't, because you have a space char before and after the field on file1
# cat file1
KEY|VIN|SEQUENCE|COST
101 | XXX111 | 1 | 234.22
234 | XXX111 | 2 | 134.32
444 | ABC234 | 1 | 100.22
555 | DFF611 | 1 | 734.82
# cat file2
XXX111
DFF611
# awk 'NR==FNR{++a[$0]}a[$3]' file2 file1
101 | XXX111 | 1 | 234.22
234 | XXX111 | 2 | 134.32
555 | DFF611 | 1 | 734.82
# awk -F"|" 'NR==FNR{a[$0]}{if($2 in a){print}} file2 file1
> ^C
# awk -F"|" 'NR==FNR{a[$0]}{if($2 in a){print}}' file2 file1
.... no output.....
Record " XXX111 "
will never match "XXX111"
danmero,
Yes, you are right, if there is a space it will not work.
This should do it
awk 'NR==FNR{a[$0]}{if($3 in a){print}}' file2 file1
regards,
Ahamed
ahamed101:
danmero,
Yes, you are right, if there is a space it will not work.
This should do it
awk 'NR==FNR{a[$0]}{if($3 in a){print}}' file2 file1
You can replace {if($3 in a){print}}
by $3 in a
zedex
May 18, 2011, 1:56pm
8
Hi
i was just wondering, why no one suggested grep
grep -f file2 file1 > newfile
I havent tried above solution but i think it should work ( at least according to grep man page )
grep would not work because requirement states to match a specific column, grep will match any line regarless of column.