All Articles

The ABC's of grep - V

V

Use the -v​ flag to invert​ what is being matched. In other words, give me the opposite of what I am trying to search for.

Let’s go back to our test.txt file with the following content:

sad
happy
awake
coffee
work
school

As before, if we wanted to search for “happy” we would do the following:

$ grep "happy" test.txt
happy

Using the -v flag will give us all the lines that do not​ contain the word “happy”.

$ grep -v "happy" test.txt
sad
awake
coffee
work
school

Use -V to print out the version of grep.

$ grep -V
grep (GNU grep) 3.6
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others; see
<https://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.

Published Nov 22, 2023

I love coffee, coding and writing.