Linux Labs

Bash: text processing and scripts / Stage 1 of 9

Regular expressions in grep

Not running Starting usually ~20 s
Leave the lesson

Step 6 of 6 · summary · 2 min

Summary

  • grep looks for the pattern's characters in a row anywhere in a line, including inside another word.
  • Where a match sits is set by ^ (start of line), $ (end) and the -w option (whole word); square brackets are one character from a set.
  • grep -E understands | and +; the pattern 'ERROR|Error:|err=[1-9]' finds every error in the log and nothing else.
  • -o prints only the matches, and a sort | uniq -c pipeline counts them.
  • At the start of the lesson, grep error found the word terror and missed ERROR, Error: and err=5: the pattern was an exact substring and set neither the place of the match, nor case, nor variants.
Ubuntu 24.04
Loading…