0
0
Linux CLIscripting~15 mins

grep with regex (-E) in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Using grep with Extended Regular Expressions (-E)
📖 Scenario: You work as a system administrator. You have a log file with many lines. You want to find lines that match certain patterns quickly.
🎯 Goal: Learn how to use grep -E to search for lines matching multiple patterns using extended regular expressions.
📋 What You'll Learn
Use the grep command with the -E option
Search for lines matching multiple patterns using the pipe | symbol
Use a sample text file with specific lines
Display the matching lines as output
💡 Why This Matters
🌍 Real World
System administrators often need to search logs quickly for errors or warnings to troubleshoot issues.
💼 Career
Knowing how to use grep with extended regex helps in filtering important information from large text files efficiently.
Progress0 / 4 steps
1
Create a sample log file
Create a file called log.txt with these exact lines:
error: disk full
warning: low memory
info: system rebooted
error: network down
info: user login
Linux CLI
Need a hint?

Use echo -e with newline characters \n and redirect output to log.txt.

2
Set the search pattern variable
Create a variable called pattern and set it to the string "error|warning" to match lines containing either 'error' or 'warning'.
Linux CLI
Need a hint?

Use pattern="error|warning" to store the regex pattern.

3
Use grep with -E and the pattern variable
Use the command grep -E "$pattern" log.txt to search log.txt for lines matching the pattern stored in pattern.
Linux CLI
Need a hint?

Use grep -E "$pattern" log.txt to find matching lines.

4
Display the matching lines
Run the script to display all lines from log.txt that contain either 'error' or 'warning'.
Linux CLI
Need a hint?

The output should show only lines with 'error' or 'warning'.