Thursday, October 28, 2021

[SOLVED] Exact grep -f command in Linux

Issue

I have 2 txt files in Linux.

A.txt contents (each line will contain a number):

1
2
3

B.txt contents (each line will contain a number):

1
2
3
10
20
30

grep -f A.txt B.txt results below:

1
2
3
10
20
30

Is there a way to grep in such a way I will get only the exact match, i.e. not 10, 20, 30?

Thanks in advance


Solution

For exact match use -x switch

grep -x -f A.txt B.txt 

EDIT: If you don't want grep's regex capabilities and need to treat search pattern as fixed-strings then use -F switch as:

grep -xF -f A.txt B.txt


Answered By - anubhava