0
0
Linux CLIscripting~15 mins

grep (search text patterns) in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Using grep to Search Text Patterns
📖 Scenario: You are working with a simple text file that contains a list of fruits and their colors. You want to find specific fruits by searching for text patterns inside this file.
🎯 Goal: Learn how to use the grep command to search for text patterns in a file and display matching lines.
📋 What You'll Learn
Create a text file named fruits.txt with exact content
Use a variable to store the search pattern
Use grep with the variable to find matching lines
Display the matching lines as output
💡 Why This Matters
🌍 Real World
Searching text files for specific words or patterns is common when working with logs, configuration files, or data files.
💼 Career
Knowing how to use grep helps in troubleshooting, data analysis, and automating tasks in many IT and software roles.
Progress0 / 4 steps
1
Create the text file fruits.txt
Create a file named fruits.txt with these exact lines:
apple red
banana yellow
cherry red
grape purple
lemon yellow
Linux CLI
Need a hint?

Use echo with -e and redirect output to fruits.txt.

2
Set a variable for the search pattern
Create a variable called pattern and set it to the exact string red.
Linux CLI
Need a hint?

Use pattern=red to assign the string.

3
Use grep with the variable to find matching lines
Use the grep command with the variable $pattern to search inside fruits.txt and show matching lines.
Linux CLI
Need a hint?

Use grep "$pattern" fruits.txt to search using the variable.

4
Display the matching lines
Run the script to display all lines from fruits.txt that contain the word red.
Linux CLI
Need a hint?

The output should show only lines containing 'red'.