0
0
Bash Scriptingscripting~15 mins

Extended regex (grep -E) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Filter Log Entries Using Extended Regex with grep -E
📖 Scenario: You have a system log file with various entries. You want to find all lines that mention either error or warning messages using a single command.
🎯 Goal: Build a bash script that uses grep -E with an extended regular expression to filter lines containing either error or warning from a log file.
📋 What You'll Learn
Create a variable logfile with the exact filename system.log
Create a variable pattern with the exact extended regex 'error|warning'
Use grep -E with $pattern and $logfile to filter matching lines
Print the filtered lines to the terminal
💡 Why This Matters
🌍 Real World
System administrators often need to scan log files for errors or warnings quickly. Using extended regex with grep helps filter relevant lines efficiently.
💼 Career
Knowing how to use grep with extended regex is a fundamental skill for troubleshooting and monitoring systems in IT and DevOps roles.
Progress0 / 4 steps
1
Create the log file variable
Create a variable called logfile and set it to the string system.log.
Bash Scripting
Need a hint?

Use single quotes around the filename to assign it as a string.

2
Create the extended regex pattern variable
Create a variable called pattern and set it to the extended regex string 'error|warning'.
Bash Scripting
Need a hint?

Use single quotes to keep the pipe character literal in the regex pattern.

3
Use grep -E with the pattern and logfile
Write a command using grep -E with the variables $pattern and $logfile to filter lines containing either 'error' or 'warning'.
Bash Scripting
Need a hint?

Use double quotes around variables to preserve their values in the command.

4
Print the filtered lines
Run the command to print the filtered lines containing 'error' or 'warning' from system.log.
Bash Scripting
Need a hint?

Make sure your system.log file contains lines with 'error' and 'warning' to see output.