Inverting, Counting, and Combining
Inverting a search is underrated. grep -v INFO app.log shows everything that is not routine information, which is usually exactly what you want from a log. You are rarely looking for the normal lines. You are looking for what is left when you remove them.
Counting answers different questions than listing. grep -c ERROR app.log tells you how many error lines there are without printing them. When the answer is 3 you probably want to read them. When it is 4000 you have learned something more useful than any individual line would have told you.
✦ The exit code is why grep works in scripts
grep exits 0 when it found something and 1 when it did not. That is what makes grep -q pattern file && echo found work, and it is why grep is the single most common command on the left of a double ampersand. Searching is not just for humans reading output, it is how scripts ask questions.
Mark this lesson complete
Signed in, your progress follows you to every device.