|
|
|
Regular expressions are powerful patterns for matching text. Shell uses regex through commands like grep, sed, and awk. |
|
|
Create sample data for demonstrations: |
|
|
Basic grep - find lines matching a pattern: |
|
|
Case-insensitive search with -i: |
|
|
Basic regex metacharacters: |
|
|
. matches any single character |
|
|
|
|
^ matches start of line |
|
|
$ matches end of line |
|
|
Character classes with []: |
|
|
Negated character class with [^]: |
|
|
Extended regex with -E (or egrep): |
|
|
Grouping with (): |
|
|
Word boundaries: |
|
|
grep -o shows only matching part: |
|
|
grep -v inverts match (show non-matching): |
|
|
grep -c counts matches: |
|
|
grep -n shows line numbers: |
|
|
Using sed for search and replace: |
|
|
sed with capture groups: |
|
|
Using awk for regex: |
|
|
Common regex patterns: |
|
|
Bash
Bash’s =~ operator for regex matching: |
|
|
Capture groups: |
|
|
Validate input with regex: |
|
|
Cleanup |
|