Compare columns in two text files and match lines

会有一股神秘感。 提交于 2019-12-11 11:58:20

问题


I want to compare the second column (delimited by a whitespace) in file1:

n01443537/n01443537_481.JPEG n01443537
n01629819/n01629819_420.JPEG n01629819
n02883205/n02883205_461.JPEG n02883205

With the second column (delimited by a whitespace) in file2:

val_8447.JPEG n09256479
val_68.JPEG n01443537
val_1054.JPEG n01629819
val_1542.JPEG n02883205
val_8480.JPEG n03089624

If there is a match, I would like to print out the corresponding line of file2.

Desired output in this example:

val_68.JPEG n01443537
val_1054.JPEG n01629819
val_1542.JPEG n02883205

I tried the following, but the output file is empty:

awk -F' ' 'NR==FNR{c[$2]++;next};c[$2] > 0' file1.txt file2.txt > file3.txt

Also tried this, but the result was the same (empty output file):

awk 'NR==FNR{a[$2];next}$2 in a' file1 file2 > file3.txt

回答1:


Using awk:

awk 'FNR==NR{a[$NF]; next} $NF in a' file1 file2

val_68.JPEG n01443537
val_1054.JPEG n01629819
val_1542.JPEG n02883205

Here is a grep alternative with process substitution:

grep -f <(awk '{print " " $NF "$"}' file1) file2

Using print " " $NF "$" to create a regex like " n01443537$" so that we match only last column in grep.




回答2:


GNU join exists for this purpose.

join -o "2.1 2.2" -j 2 <(sort -k 2 file1) <(sort -k 2 file2)



来源:https://stackoverflow.com/questions/36270169/compare-columns-in-two-text-files-and-match-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!