How to use grep command to get fixed pattern or exact keyword

How to use grep command to get fixed pattern or exact keyword

In this post this is a small trick about How to use grep command to get fixed pattern or exact keyword in files.

grep command is useful in searching the keyword in files.
Without using any flag the grep command will show the lines in file which is matching with keyword.
For eg. If I want to search for keyword “test” it will show me the results similar matching with keyword “test” like testing,loadtesting ,test.

Below is sample of grep command output. In this eg. you can see it shows the result with similar words not exact word.

linux@tuxworld:/tmp$ cat red 
this is a test
file for doing testing
by redtest team
this is called redtesting
bla bla bla 
bla 2 bla 3 bla 4
ok ok test the file
linux@tuxworld:/tmp$ 
linux@tuxworld:/tmp$ 
linux@tuxworld:/tmp$ grep test red
this is a test
file for doing testing
by redtest team

How to find the exact keyword in files

For getting exact pattern of word or keyword. Use -w flag.

grep -w keyword fileName

or 

cat filename | grep -w keyword

See the given below example , Here we are only searching for test keyword

linux@tuxworld:/tmp$ cat red
this is a test
file for doing testing
by redtest team
this is called redtesting
bla bla bla 
bla 2 bla 3 bla 4
ok ok test the file
linux@tuxworld:/tmp$ 
linux@tuxworld:/tmp$ grep -w test red
this is a test
ok ok test the file
linux@tuxworld:/tmp$

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.