grep -i, -n, -v & -c
Search inside files
Lesson 9 of 20 · 9 min · Module 3: Text superpowers
Logs can be thousands of lines long. grep finds the lines that matter in a fraction of a second.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Find matching lines
- Control matching with options
- Quote a pattern that contains spaces
The idea
grep word file prints every line containing word. Useful options: -i ignores upper/lower case, -n adds line numbers, -v shows lines that do not match, and -c counts matching lines instead of printing them.
grep ERROR logs/app.log
grep -c INFO logs/app.log
01grep matches text, not words
grep ERROR finds any line that contains those letters, in that exact case. Add -i to also find error and Error.
grep -i error logs/app.log | head -n 12026-10-18 08:07:03 ERROR Payment service timeout02Quote patterns with spaces
Without quotes, grep signed in app.log looks for signed inside files named in and app.log. With quotes, "signed in" is one pattern.
03Count, number, or invert
-c answers "how many?", -n answers "where?", and -v answers "what else?". grep -v INFO quickly hides routine messages so the problems stand out.
Your turn
ERROR in logs/app.log, with line numbers. Then count how many lines contain signed in.What the checks look for
- Shows ERROR lines with line numbers
- Counts 3 sign-ins
Take this with you
grep finds the needle; options decide how it is reported.
Go a little further
Combine options: grep -in error logs/app.log finds every case of error and shows where.