Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

awk - Match two columns and put them in one file

I have two files. In each file I have two columns. I need to match the first value of column two of file1 with each value from column two of file2. If they are equal I need to put the two columns (column one from file1 and file2) in one file, but they should be adjacent to each other. If the two values do not match, do nothing.

file1

344  0
465  1
729  2
777  3
676  4
862  5

file2

766  0
937  1
980  2
837  3
936  5

Example output:

344    766
465    937
729    980
777    837
862    936
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you insist this be done in awk

awk 'NR == FNR {arr[$2] = $1; next} {if ($2 in arr){print(arr[$2], $1)}}' file1 file2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...