0
0
Bash Scriptingscripting~15 mins

grep in scripts in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using grep in Bash Scripts
📖 Scenario: You work as a system administrator. You often need to find specific words in log files to check system status or errors.Today, you will write a simple bash script that uses grep to search for a word in a text file.
🎯 Goal: Create a bash script that searches for a specific word in a file using grep and shows the matching lines.
📋 What You'll Learn
Create a variable holding the filename
Create a variable holding the search word
Use grep to find the word in the file
Print the matching lines
💡 Why This Matters
🌍 Real World
System administrators often need to search logs for errors or specific messages quickly using scripts.
💼 Career
Knowing how to use <code>grep</code> in scripts helps automate monitoring and troubleshooting tasks in IT jobs.
Progress0 / 4 steps
1
Create the text file
Create a file called log.txt with these exact lines:
error: disk full
info: system rebooted
warning: high memory usage
error: network down
Bash Scripting
Need a hint?

Use cat with a here-document to create the file.

2
Set variables for filename and search word
Create a variable called filename and set it to log.txt.
Create a variable called search_word and set it to error.
Bash Scripting
Need a hint?

Use = without spaces to assign values to variables.

3
Use grep to find the search word in the file
Use grep with the variable search_word to search inside the file stored in filename. Store the result in a variable called result.
Bash Scripting
Need a hint?

Use command substitution $( ) to save grep output to a variable.

4
Print the matching lines
Print the variable result to show the lines containing the search word.
Bash Scripting
Need a hint?

Use printf or echo to print the variable.