Ubuntu – How to grep same strings by comparing two files

command linetext processing

I have two files file A and file B

File A contains all the information while file B contains unique information ids. What I want to do is to compare both files and grep the ids information from file A

file 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

file B:

A1S_1865
A1S_1774
A1S_1116
A1S_0106
A1S_2677

desired output:

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

Best Answer

With grep:

grep -Ff fileB fileA

-f <filename> tells grep to read patterns from file, and -F makes it treat the patterns as fixed strings instead of regular expressions. (This is assuming the IDs don't show up in the other column.)