Advanced Grep with Regex
Apply complex regex patterns with 'grep' to perform highly specific and efficient text searches within files.
Advanced Grep with Regex is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Grep's Regex Superpowers
Welcome to Advanced Grep with Regex! In the previous lesson, we learned about Regular Expressions (Regex) themselves. Now, let's unlock their full potential within the powerful grep command.
grep is your go-to tool for searching text patterns in files. By combining it with advanced regex, you can pinpoint exactly what you're looking for, no matter how complex the pattern.
Enabling Extended Regex (-E)
By default, grep uses Basic Regular Expressions (BRE). To use more advanced features like +, ?, |, and (), we need to enable Extended Regular Expressions (ERE).
You can do this using the -E option or by using the egrep command, which is essentially grep -E. Let's create a sample file to work with:
echo "apple
banana
appanana
orange
app" > fruits.txtQuantifiers: One or More (+)
The + quantifier matches one or more occurrences of the preceding character or group. It's super useful when you expect a pattern to repeat at least once.
For example, to find lines with 'p' followed by one or more 'a's, we'd use pa+.
grep -E 'pa+' fruits.txtQuantifiers: Zero or More (*)
The * quantifier matches zero or more occurrences of the preceding character or group. This is useful when a character might be present, but isn't strictly required.
Let's find lines with 'b' followed by zero or more 'a's:
grep -E 'ba*' fruits.txtQuantifiers: Zero or One (?)
The ? quantifier matches zero or one occurrence of the preceding character or group. Think of it as making a character optional.
Want to find both 'color' and 'colour'? You can make the 'u' optional:
echo "color
colour
coor" > words.txt
grep -E 'colou?r' words.txtSpecific Counts with {} (Curly Braces)
For even more control, you can specify exact counts or ranges using curly braces {}:
a{3}: Exactly three 'a's.a{2,}: Two or more 'a's.a{1,3}: Between one and three 'a's.
Let's find lines with 'a' repeated exactly two times:
grep -E 'a{2}' fruits.txtAnchors: Start and End (^, $)
Anchors don't match characters; they match positions. The ^ anchor matches the start of a line, and $ matches the end of a line.
This is crucial for ensuring your pattern matches the entire line or begins/ends precisely where you intend.
^apple: Lines starting with 'apple'.banana$: Lines ending with 'banana'.^apple$: Lines containing only 'apple'.
grep -E '^app' fruits.txtAlternation with OR (|)
The | (pipe) symbol acts as an OR operator. It allows you to match one pattern OR another. This is incredibly useful for searching for multiple distinct words or phrases at once.
For example, to find lines containing either 'apple' or 'orange':
grep -E 'apple|orange' fruits.txtGrouping Patterns with ()
Parentheses () are used for grouping parts of a regular expression. This allows you to apply quantifiers or alternation to an entire group, not just a single character.
For instance, to find 'ana' appearing one or more times after 'b':
grep -E 'b(ana)+' fruits.txtQuick Check: Advanced Grep
Given the file data.txt containing:
cat
cats
catt
catterpillar
dog
What command would correctly find lines that contain 'cat' followed by zero or more 't's?
Recap: Grep's Regex Power
You've mastered advanced grep techniques using Extended Regular Expressions! We covered:
- Using
-Efor extended regex features. - Quantifiers:
+(one or more),*(zero or more),?(zero or one), and{}for specific counts. - Anchors:
^(start of line) and$(end of line). - Alternation
|for 'OR' conditions. - Grouping patterns with
().
These tools allow you to perform incredibly precise and flexible text searches, making you a true command-line wizard!
Frequently asked questions
Is the “Advanced Grep with Regex” lesson free?
Yes — the full text of “Advanced Grep with Regex” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Advanced Grep with Regex”?
Apply complex regex patterns with 'grep' to perform highly specific and efficient text searches within files. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Advanced Grep with Regex” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.