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.
More on Text processing: grep, sed, awk, cut, sort, uniq
What are the most useful `grep` flags?BeginnerWhat are the core `sed` commands you use in automation?BeginnerWhy does `sed -i` behave differently on macOS and Linux?IntermediateHow do you use variables inside a `sed` expression safely?AdvancedWhat is the structure of an `awk` program?BeginnerHow do you count occurrences per key with `awk` associative arrays?Intermediate