Sikau

Linux and the shell · Day 7 · about 30 min

Searching text with grep

grep options and regular expressions.

Find the line that matters

A log has thousands of lines; you need the three errors. grep prints lines matching a pattern — a plain word or a regular expression — and it's the tool you'll use most when debugging servers.

grep's options

grep ERROR app.log
grep -i error app.log
grep -c -i error app.log
grep -n WARN app.log
grep -v INFO app.log
grep -E "ERROR|WARN" app.log
grep -o "order=[0-9]*" app.log
grep -E "[0-9]{4}ms" app.log
grep -r "console" ~/src
  • . any character, * repeat, + one or more (with -E)
  • ^ start of line, $ end of line
  • [abc] one of, [^abc] none of, \b word boundary

The rest of this lesson

  • grep with contextexplain
  • Regular expressions you'll actually useexplain
  • Which lines?predict
  • grep optionmatch
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 16 lessons of Linux and the shell