开发者

Matching two files with awk codes

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜