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, you need to prepare a summary of system.log: how many warnings were logged, what non-routine events occurred, and a copy filed for the next shift.
- Count how many lines contain
warning, just the number, not the lines themselves. - Print every line that does not contain
info: the non-routine events. - Print the
errorlines with a line number in front of each, so the night crew can jump straight to them. - Create a folder called
handoffand copysystem.loginto it asnight-brief.log.
Expected result
You see the warning count, the filtered non-routine lines, the numbered error line, and a copy of the log waiting in handoff.