How to exclude matching lines with grep

Known-good status lines, debug rows, and repeated noise can hide the rows that need review. Use grep -v when the useful output is every line that does not match a chosen pattern.

The -v option inverts normal grep selection, so grep prints non-matching lines instead of matching lines. The pattern still follows normal grep matching rules, which means an unanchored word can exclude any line containing that word.

The sample file uses log levels at the start of each line so the excluded pattern can be anchored with ^INFO. The expected proof is that the ordinary grep command prints only INFO rows, while grep -v returns the warning, error, and debug rows that do not begin with INFO.

Steps to exclude matching lines with grep:

  1. Create a short sample log file with several status levels.
    $ cat > app.log <<'EOF'
    INFO health check passed
    INFO cache warmed
    WARN disk usage 82 percent
    ERROR payment worker failed
    DEBUG retry scheduled
    EOF
  2. Run a normal grep search to confirm which lines match the pattern.
    $ grep '^INFO' app.log
    INFO health check passed
    INFO cache warmed

    The ^ anchor keeps the pattern tied to the start of the line, so only log lines whose level starts with INFO are selected.

  3. Add -v to print every line that does not match the same pattern.
    $ grep -v '^INFO' app.log
    WARN disk usage 82 percent
    ERROR payment worker failed
    DEBUG retry scheduled
  4. Exclude more than one pattern by repeating -e for each pattern.
    $ grep -v -e '^INFO' -e '^DEBUG' app.log
    WARN disk usage 82 percent
    ERROR payment worker failed

    With multiple patterns, -v prints only lines that match none of the listed patterns.

  5. Remove the sample file after testing.
    $ rm app.log