All Articles

The ABC's of grep - W

W

Use the -w​ flag to match on whole words.

Recall our test.txt file with the following content:

sad
happy
awake
coffee
work
school

Suppose we want to search for “wake”. We would do the following:

$ grep "wake" test.txt
awake

Using the -w flag will give us all the lines that contain “wake” as a whole word and not just a part of word.

$ grep -w "wake" test.txt

In this case, the grep command returns nothing.

Published Nov 23, 2023

I love coffee, coding and writing.