Interview Prep/Shell Scripting/Text processing: grep, sed, awk, cut, sort, uniq

What is the difference between basic, extended and Perl regular expressions in grep?

IntermediateConceptText processing: grep, sed, awk, cut, sort, uniq
Answer
  • BRE (grep): ?, +, {}, |, () are literal unless backslashed (\+, \{3\}, \|).
  • ERE (grep -E): those are operators directly.
  • PCRE (grep -P, GNU only): adds \d, \s, lazy quantifiers, lookarounds.
grep -E '^[0-9]{1,3}(\.[0-9]{1,3}){3}$' ips.txt
grep -oP '(?<=request_id=)[a-f0-9-]+' app.log      # lookbehind
grep -F '[ERROR]' app.log                           # brackets literal, fastest

\d does not work in grep -E; use [0-9]. macOS grep has no -P.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Text processing: grep, sed, awk, cut, sort, uniq