Use the -B
flag to show the lines before the matching search term is found.
Remember our file test.txt
with the following content:
sad
happy
awake
coffee
work
school
As before, we would search for the word “happy” like this:
$ grep "happy" test.txt
happy
To see the first line before happy, execute the following:
$ grep -B 1 "happy" test.txt
sad
happy
Want to see this in action? Check out the video below.