如何在没有 grep -w 的情况下从文件中提取完全匹配

how extract exact match from a file without grep -w

我有一个文件 A,其中包含一列字符串,如下所示:

12
123
1234
1234567

我想使用文件 A 中的字符串来 grep 文件 B 中包含它们的行,文件 B 如下所示:

1       0/0     ./.     0/1     0/0
12      0/0     0/0     1/1     ./.
1234    1/1     0/1     ./.     0/0
12345   0/0     0/0     1/1     1/1
123456  1/1     1/1     ./.     ./.

在这种情况下,我正在等待与文件 A 中的字符串完全匹配的输出,如下所示:

12      0/0     0/0     1/1     ./.
1234    1/1     0/1     ./.     0/0

我用过grep -wf A B,效果很好,但问题是我的真实文件很重,处理成本很高。有人有任何不同的想法来获得相同的结果但使用其他命令行?

您可以使用此 awk 作为替代:

awk 'NR==FNR{a[]; next}  in a' file1 file2
12      0/0     0/0     1/1     ./.
1234    1/1     0/1     ./.     0/0

解释:

NR == FNR {                  # While processing the first file
  a[]                      # store the 1st field in array a
  next                       # move to next line
}
 in a                      # while processing the second file
                             # if 1st field is in array then print it