grep Options
Plain grep is useful, but a few options make it far smarter.
Ignore case: grep -i
By default grep is case-sensitive: Error and error are different. The -i
option makes the search case-insensitive:
grep -i error system.log
This matches error, Error, ERROR, and any mix.
Count matches: grep -c
The -c option prints just the number of matching lines, not the lines
themselves:
grep -c warning system.log
3
Invert the match: grep -v
The -v option flips the search: it prints lines that do NOT contain the word:
grep -v info system.log
This hides every info line and shows everything else.
Show line numbers: grep -n
The -n option puts the line number in front of each match:
grep -n error system.log
5:error: sensor 3 offline
Options combine, just like with ls: grep -in error system.log is case-insensitive
and numbered.
Mission: Shift-End Log Summary
Before handing off to the night crew, sum up system.log with three searches:
- How many routine
infolines were logged? The number alone, not the lines. - Everything that is not a
warning, so the night crew sees the rest at a glance. - The
warninglines, each with its position in the file, so the crew can jump straight to them.
Then file a copy of the log for the next shift as handoff/night-brief.log.
Expected result
You see a single number, then the lines with no warning in them, then the warnings with their line numbers, and the copy waits in handoff.