开发者

Matching two files with awk codes

开发者 https://www.devze.com 2023-04-07 12:17 出处:网络
There are two files first.file M1 M2 M3 ... second.file A1 M1 A2 M1 A2 M3 A3 M2 开发者_如何转开发A3 M4

There are two files

first.file

  • M1
  • M2
  • M3

...

second.file

....

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号