Skip to content
Latchkey

grep -v: Invert Match to Filter Lines Out

grep -v prints every line that does not match the pattern, the inverse of normal grep.

In pipelines, -v is the standard way to strip noise (debug lines, known-benign warnings) before inspecting what remains.

What it does

grep -v (--invert-match) selects the non-matching lines. It is commonly chained to remove several kinds of noise in turn, or combined with a second grep to assert that only allowed content remains.

Common usage

Terminal
# drop debug noise from a log
grep -v DEBUG app.log

# remove blank lines and comments
grep -v '^#' config.ini | grep -v '^$'

# fail if any non-INFO/WARN line appears
grep -vE 'INFO|WARN' app.log && exit 1 || true

Options

FlagWhat it does
-v / --invert-matchPrint lines that do not match
-EUse extended regex (for alternation in the exclude)
-cCount the non-matching lines
-iCase-insensitive invert

In CI

A useful pattern is "fail if anything unexpected remains": filter out every known-good line with -v, and if any line is left, treat the build as failed. Be careful with the exit code, since grep -v exits 0 when it printed at least one non-matching line, which inverts your intuition about success.

Common errors in CI

A gate built on grep -v can pass when you expected it to fail because -v exits 0 as long as one line did not match, which is almost always true. Drive the decision off whether output is empty (e.g. pipe to wc -l or test with [ -s file ]) rather than off the exit code alone.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →