Use the -x
flag to match on an entire line.
Let’s go back to our test.txt
file and add a sentence at the end:
sad
happy
awake
coffee
work
school
I love coffee.
If we wanted to search for “coffee” we would do the following:
$ grep "coffee" test.txt
coffee
I love coffee.
As expected, we get both lines that contain the word “coffee”.
Let’s try again with the -x
flag.
$ grep -x "coffee" test.txt
coffee
$ grep -x "I love coffee." test.txt
I love coffee.
As you can see, using the -x
flag only matches on the entire line.