如何将行的开头与 id 文件中的模式匹配?

How to match the beginning of a line with patterns from id file?

我想用我的 id_file 搜索我的 big_file 提取与 id 匹配的行行的开头 big_file.

我是初学者,正在努力使用 grep(版本 grep (BSD grep) 2.5.1-FreeBSD)并了解下面引用的解决方案。

我的 id_file 包含 id:

67b
84D
118
136
166

我的 big_file 看起来像这样:

118 ABL1_BCR
118 AC005258
166 HSP90AB1
166 IKZF2_SP
166 IL1RAP_D
136 ABL1_BCR
136 ABL1_BCR
555 BCR_136
555 BCR_136
555 BCR_136
59  UNC45B_M 166
59  WASF2_GN 166
59  YPEL5_CX 166

正如克里斯·西摩所建议的那样here

尝试 1:我用过

grep -wFf id_file big_file

这显然不起作用,因为数字出现在 big_file.

行的其他地方

尝试2:我修改了id_file;

^67b
^84D
^118
^136
^166

又是运行grep -wFf id_file big_file

当然,那也没用

我查看了 batimar 的意见 here,但我未能执行该建议。

Better usage is taking only some patterns from some file and this patterns use for your file

grep '^PAT' patterns.txt | grep -f - myfile

This will take all patterns from file patterns.txt starting with PAT and use this patterns from the next grep to search in myfile.

我试图用我的示例以多种方式重现上面的代码,但显然我只是不明白它们的意思,因为它 none 有效。

我的修补 1 有 2 个结果:No such file or directory 或根本没有输出。

有没有办法只用 grep 来做到这一点?

如果有人能为我分解它,我将不胜感激。

这似乎是 BSD grep 的问题。看 https://unix.stackexchange.com/questions/352977/why-does-this-bsd-grep-result-differ-from-gnu-grep 类似问题。

您可以使用 awk 作为替代方案(这个确切的解决方案可能在某处重复):

awk 'NR==FNR{a[]; next}  in a' id_file large_file
  • NR==FNR{a[]; next}id_file 的第一个字段作为键构建一个关联数组
  • 如果来自 large_file 的行的第一个字段与数组 a 中的任何键匹配,则
  • in a 将为真。如果是这样,将打印整行。

按照 OP“尝试 2”中所述使用 id_file

^67b
^84D
^118
^136
^166

然后试试这个:

fname="id_file”; lines=$(cat $fname); for line in $lines; do grep $line big_file >> filtered_output; done