Linux Essentials Preparation · lesson 12 of 23 · 25 min
Regular Expression Basics
Regular expressions (regex) are patterns used to match text. Used with grep, sed, awk, and many programming languages.
Basic regex metacharacters:
. — matches any single character (except newline).
***** — matches zero or more of the preceding character.
^ — anchors to the start of a line.
$ — anchors to the end of a line.
[ ] — character class. [abc] matches a, b, or c. [a-z] matches any lowercase letter.
[^ ] — negated character class. [^abc] matches anything except a, b, c.
— escape character. . matches a literal dot.
Extended Regex and Tools
Extended regex (ERE) adds: + (one or more), ? (zero or one), | (alternation), ( ) (grouping), {n,m} (repetition). Use with grep -E or egrep.
grep — search for patterns in files.
grep "pattern" file — basic search.
grep -i "pattern" file — case-insensitive.
grep -v "pattern" file — invert match (lines NOT matching).
grep -r "pattern" /dir — recursive search.
grep -n "pattern" file — show line numbers.
grep -E "pat1|pat2" file — extended regex (OR).
sed — stream editor. Substitution: sed 's/old/new/g' file. The g flag replaces all occurrences. sed -i edits in place.
awk — pattern scanning and processing. Processes text field by field. awk '{print $1}' file prints the first field. awk -F: '{print $1}' /etc/passwd uses : as delimiter.
Exam Tip
grep -v inverts the match. grep -i is case-insensitive. grep -E enables extended regex. sed ‘s/old/new/g’ replaces all occurrences. awk prints fields by number ($1, $2). -F sets the field delimiter.
Key Terms
Key Terms
Knowledge Check — 8 Questions
Linux Essentials — Regular Expressions
grep -v “error” logfile shows:
- Lines containing “error”
- Lines NOT containing “error”
- The count of error lines
- Error lines in red
- Answer: Lines NOT containing “error”
grep -v inverts the match — shows all lines that do NOT match the pattern. The regex ^root matches:
- Lines containing “root” anywhere
- Lines starting with “root”
- Lines ending with “root”
- The word “root” exactly
- Answer: Lines starting with “root”
^ anchors to the start of a line. ^root matches lines where “root” appears at the very beginning. sed ‘s/foo/bar/g’ file replaces:
- The first occurrence of foo on each line
- All occurrences of foo with bar
- foo only on the first line
- foo with bar only if foo is a whole word
- Answer: All occurrences of foo with bar
The g flag in sed substitution means global — replace all occurrences on each line. Without g, only the first occurrence per line is replaced. awk -F: ‘{print $1}’ /etc/passwd prints:
- The entire first line
- The first field (username) from each line
Lines containing :
- The file header
- Answer: The first field (username) from each line
-F: sets the field delimiter to colon. $1 is the first field — in /etc/passwd that is the username. grep -E “cat|dog” file matches lines containing:
- cat and dog on the same line
- cat or dog
- The literal string “cat|dog”
- Neither cat nor dog
- Answer: cat or dog
grep -E enables extended regex. | is alternation (OR) — matches lines containing cat OR dog. The character class [0-9] matches:
- The string “0-9”
- Any single digit
- Zero or nine only
- Any number
- Answer: Any single digit
[0-9] is a character class matching any single digit from 0 to 9. [a-z] matches any lowercase letter. grep -r “password” /etc searches:
- Only /etc/passwd
- All files in /etc recursively
- Only the first level of /etc
- Binary files only
- Answer: All files in /etc recursively
grep -r (recursive) searches all files in the specified directory and all subdirectories.
The regex anchor _____ matches the end of a line.
Answer: $
$ anchors to the end of a line. bash$ matches lines ending with “bash”. ^ anchors to the start.
Previous lesson: Shell scripting basics: variables, conditionals, loops, functions, shebang
Back to Linux Essentials Preparation
Next lesson: Environment variables: PATH, HOME, USER, SHELL; export, env, set, unset