Exact grep -f command on Linux

I have 2 txt files on Linux.

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

1 2 3 

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

 1 2 3 10 20 30 

grep -f A.txt B.txt below:

 1 2 3 10 20 30 

Is there a way to grep this way, I only get an exact match, i.e. not 10, 20, 30?

Thank you in advance

+4
source share
4 answers

For exact match, use the -x switch

 grep -x -f A.txt B.txt 

EDIT: If you do not want to use the grep regular expression functions and must treat the search pattern as fixed lines, use -F switch as:

 grep -xF -f A.txt B.txt 
+8
source

As Anubhava noted, grep -x will match the entire line. there is another -w for the matching word. So grep -wf A.txt B.txt will display matches if a word from A.txt matches a word in B.txt

+3
source

grep -wf A.txt B.txt

This will give you an accurate result that is under: 1 2 3

thanks

+2
source

you can try to determine a file name containing other content

  # cat a.txt 1 2 3 # cat b.txt 1 2 3 10 20 30 # grep -L a.txt b.txt b.txt 
-1
source

Source: https://habr.com/ru/post/1482307/


All Articles