Compare columns in two text files and match lines

2019-08-12 16:57发布

问题:

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)