如何通过比较两个文件来grep相同的字符串


9

我有两个文件A和B

文件A包含所有信息,而文件B包含唯一的信息ID。我想要做的是比较两个文件,并从文件A中获取grep的ID信息

档案A:

acb:A1S_1863    ncbi-proteinid:ABO12290
acb:A1S_1864    ncbi-proteinid:ABO12291
acb:A1S_1865    ncbi-proteinid:ABO12292
acb:A1S_0105    ncbi-proteinid:ABO10592
acb:A1S_0106    ncbi-proteinid:ABO10593

文件B:

A1S_1865
A1S_1774
A1S_1116
A1S_0106
A1S_2677

所需的输出:

acb:A1S_1865    ncbi-proteinid:ABO12292
acb:A1S_0106    ncbi-proteinid:ABO10593

Answers:


15

使用grep:

grep -Ff fileB fileA

-f <filename>告诉grep从文件中读取模式,并-F使其将模式视为固定字符串而不是正则表达式。(这是假设ID未显示在另一列中。)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.