📖 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
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
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
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
Hint
Use printf or echo to print the variable.
Practice
(1/5)
1. What does the grep command do in a bash script?
easy
A. Searches for lines matching a pattern in files or input
B. Deletes files matching a pattern
C. Creates new files with matching names
D. Changes file permissions based on a pattern
Solution
Step 1: Understand grep's purpose
The grep command is used to search text for lines that match a given pattern.
Step 2: Compare options
Options B, C, and D describe actions unrelated to searching text, so they are incorrect.
Final Answer:
Searches for lines matching a pattern in files or input -> Option A
Quick Check:
grep = search text lines [OK]
Hint: grep finds text patterns in files or input quickly [OK]
Common Mistakes:
Thinking grep deletes or modifies files
Confusing grep with commands like rm or chmod
Assuming grep creates files
2. Which of the following is the correct syntax to search for the word "error" in a file named log.txt using grep?
easy
A. grep -f error log.txt
B. grep error log.txt
C. grep log.txt error
D. grep -c log.txt error
Solution
Step 1: Identify grep syntax
The correct syntax is grep [pattern] [file]. So, searching for "error" in "log.txt" is grep error log.txt.
Step 2: Check other options
grep -f error log.txt uses -f which expects a file of patterns, not a pattern string. Options C and D have arguments reversed or wrong flags.
Final Answer:
grep error log.txt -> Option B
Quick Check:
grep pattern file = correct syntax [OK]
Hint: Remember: grep pattern filename [OK]
Common Mistakes:
Swapping pattern and filename order
Using -f flag incorrectly
Confusing -c flag usage
3. What will be the output of this script snippet?
echo -e "apple\nbanana\nApple" | grep -i apple
medium
A. apple
B. No output
C. apple\nApple
D. Apple
Solution
Step 1: Understand the input
The input lines are "apple", "banana", and "Apple".
Step 2: Apply grep with -i option
The -i option makes grep case-insensitive, so it matches both "apple" and "Apple" but not "banana".
Final Answer:
apple\nApple -> Option C
Quick Check:
grep -i matches case-insensitive lines [OK]
Hint: Use -i to ignore case when searching [OK]
Common Mistakes:
Ignoring case sensitivity of grep
Expecting only lowercase matches
Assuming grep outputs only one match
4. Identify the error in this script snippet:
if grep "error" logfile.txt
then
echo "Error found"
fi
medium
A. Missing semicolon or newline before then
B. grep command syntax is wrong
C. echo command should be outside if block
D. fi is missing
Solution
Step 1: Check if statement syntax
In bash, the if condition must end with a semicolon or newline before then. Here, then is on a new line and the previous line ends properly, so syntax is correct.
Step 2: Confirm other parts
grep syntax is correct, echo is correctly inside if, and fi is present.
Final Answer:
No error in the script snippet -> Option A
Quick Check:
if condition syntax is correct as given [OK]
Hint: Put ; or newline before then in if statements [OK]
Common Mistakes:
Writing if and then on same line without semicolon
Misplacing fi or echo commands
Confusing grep syntax with if syntax
5. You want to count how many lines in data.txt contain the word "success" ignoring case, and store the number in a variable count. Which script snippet correctly does this?
hard
A. count=$(grep -i success -c data.txt)
B. count=$(grep -c success -i data.txt)
C. count=$(grep success -i -c data.txt)
D. count=$(grep -ci success data.txt)
Solution
Step 1: Understand grep options
The -c option counts matching lines, and -i ignores case. Both can be combined as -ci.
Step 2: Check option order and syntax
Options A, B, and D have options separated or misplaced. count=$(grep -ci success data.txt) correctly combines -c and -i before the pattern and filename.
Final Answer:
count=$(grep -ci success data.txt) -> Option D
Quick Check:
Use combined options -ci for count and ignore case [OK]
Hint: Combine options like -ci for count and ignore case [OK]
Common Mistakes:
Separating options incorrectly
Placing options after filename
Not combining -c and -i properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.