Matching two files with awk codes
There are two files
first.file
- M1
- M2
- M3
...
second.file
- A1 M1
- A2 M1
- A2 M3
- A3 M2 开发者_如何转开发
- A3 M4
- A3 M5
....
I want to match first.file to second.file My result file should be like that:
result.file
- A1 M1
- A2 M1
- A2 M3
- A3 M2
How can I do that with awk codes ?
Thank you in advance
awk '
BEGIN { while (getline < "first.file") { file1[$0]=1 } }
$2 in file1 { print }
' <second.file
Use the below:
grep -f firstfile secondfile
grep
is enough.
even though we can do this with awk
too,i prefer grep
If you still insist on awk,Then i have a very simple solution in awk too.
awk 'FNR==NR{a[$0];next}($0 in a)' file2 file1
Explanation:
Put file2 entries into an array. Then iterate file1, each time finding those entries in the array.
精彩评论