Text Processing | Linux - Wyatt's Notes
Regular Expressions
Section titled “Regular Expressions”Regular expressions are the backbone of text processing on Linux. Three major flavors exist, each With different capabilities and syntax.
BRE vs ERE vs PCRE
Section titled “BRE vs ERE vs PCRE”| Flavor | Engine | Activator | Metacharacters Require Escape | Lookaround | | ------ | --------------- | ------------------ | ----------------------------- | ---------- | --- | | BRE | POSIX grep | Default | +?{`` |() | No | | ERE | POSIX grep -E | grep -E``egrep | None | No | | PCRE | Perl-compatible | grep -P``ripgrep | None | Yes |
BRE: \{1,3\} \+ \? \(group\)ERE: {1,3} + ? (group)PCRE: {1,3} + ? (group) + lookaround + backreferences + named groupsCharacter Classes and Anchors
Section titled “Character Classes and Anchors”Character Classes: [abc] any of a, b, c [a-z] lowercase letters [^0-9] NOT a digit [[:alpha:]] POSIX class — any letter [[:digit:]] POSIX class — any digit [[:alnum:]] letters or digits [[:space:]] whitespace [[:upper:]] uppercase letters [[:lower:]] lowercase letters
Anchors: ^ start of line (or start of string in multiline mode) $ end of line \b word boundary \B non-word boundary \< start of word (GNU extension) \> end of word (GNU extension)Quantifiers
Section titled “Quantifiers”Greedy (match as much as possible): * zero or more + one or more ? zero or one {n} exactly n {n,} n or more {n,m} between n and m
Lazy (match as little as possible — PCRE only): *? zero or more (lazy) +? one or more (lazy) ?? zero or one (lazy)Lookahead and Lookbehind (PCRE)
Section titled “Lookahead and Lookbehind (PCRE)”## Positive lookahead — match "foo" only when followed by "bar"grep -P "foo(?=bar)' file.txt
## Negative lookahead — match "foo" only when NOT followed by "bar"grep -P 'foo(?!bar)' file.txt
# Positive lookbehind — match "bar" only when preceded by "foo"grep -P '(?<=foo)bar' file.txt
# Negative lookbehind — match "bar" only when NOT preceded by "foo"grep -P '(?<!foo)bar' file.txtPractical Regex Patterns
Section titled “Practical Regex Patterns”# IPv4 addressgrep -P '(\d{1,3}\.){3}\d{1,3}' file.txt
# Email address (basic)grep -P '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' file.txt
# MAC addressgrep -P '([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}' file.txt
# UUIDgrep -P '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' file.txt
# ISO 8601 dategrep -P '\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:?\d{2})?)?' file.txt
# Semantic versiongrep -P '\bv[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?(\+[a-zA-Z0-9.]+)?\b' file.txtsed — Stream Editor
Section titled “sed — Stream Editor”sed reads input line by line, applies editing commands, and writes output. It operates on a pattern space (a working buffer holding the current line) and a hold space (a secondary Buffer for multi-line operations).
Basic Substitution
Section titled “Basic Substitution”# Replace first occurrence per linesed 's/old/new/' file.txt
# Replace all occurrences per linesed 's/old/new/g' file.txt
# Replace on lines matching a patternsed '/error/s/warning/ERROR/g' file.txt
# Replace on specific line numberssed '3s/foo/bar/' file.txt
# Replace from line 3 to 5sed '3,5s/foo/bar/' file.txt
# Replace from line 3 to endsed '3,$s/foo/bar/' file.txtAddress Ranges
Section titled “Address Ranges”# Line numberssed -n '10,20p' file.txt # print lines 10-20sed '1d' file.txt # delete first linesed '$d' file.txt # delete last line
# Pattern rangessed '/start/,/end/d' file.txt # delete block between markerssed '/ERROR/,+3d' file.txt # delete ERROR line and 3 following lines
# Step rangessed '1~2d' file.txt # delete every 2nd line (odd lines)sed '0~3d' file.txt # delete every 3rd line (lines 3, 6, 9...)
# Regex with flagssed -n '/^#.*enabled/Ip' config # case-insensitive, print matchingHold Space Operations
Section titled “Hold Space Operations”The hold space is a secondary buffer. It persists across lines, enabling multi-line transformations.
# Copy pattern space to hold space (h), append (H)# Get hold space to pattern space (g), append (G)# Exchange pattern and hold space (x)
# Reverse the order of lines in a filesed '1!G;h;$!d' file.txt
# Join every two lines into onesed 'N;s/\n/ /' file.txt
# Delete blank lines and join previous line with nextsed '/^$/N;/\n$/d' file.txt
# Double-space a filesed G file.txt
# Print the line AFTER a pattern matchsed -n '/pattern/{n;p}' file.txtBranching and Flow Control
Section titled “Branching and Flow Control”# Label and branchsed '/start/b skip; s/foo/bar/; :skip' file.txt
# Conditional branch — skip substitution on comment linessed '/^#/b; s/enabled/disabled/' config
# Loop with t (branch if substitution was made)# Remove all leading spaces (not tabs) — one at a timesed ':loop; s/^ //; t loop' file.txt
# Infinite loop with breaksed ':top; s/ / /; t top' file.txt # collapse multiple spaces to onesed Scripts
Section titled “sed Scripts”For complex operations, use a sed script file:
cat > edit.sed << 'EOF'# Comment lines are ignored by sed/^#/ds/TODO/FIXME/g1,10s/enabled/disabled//^Listen /s/80/8080/$ a \# End of processed fileEOF
sed -f edit.sed httpd.confIn-Place Editing
Section titled “In-Place Editing”# Create backup with .bak extensionsed -i.bak 's/old/new/g' file.txt
# In-place without backup (dangerous — no recovery)sed -i 's/old/new/g' file.txt
# Operate on multiple filessed -i 's/192.168.1.100/10.0.0.1/g' /etc/hosts /etc/resolv.conf