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:
- 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
- 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.
- 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
- 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.
- Remove the sample file after testing.
$ rm app.log
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.