0
0
Bash Scriptingscripting~15 mins

Basic regex in grep in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Basic regex in grep
📖 Scenario: You are working with a text file that contains a list of fruits and vegetables. You want to find lines that match certain patterns using grep with basic regular expressions.
🎯 Goal: Learn how to use grep with basic regex patterns to search for specific words or patterns in a text file.
📋 What You'll Learn
Create a text file named items.txt with exact lines provided.
Use a variable to store the filename items.txt.
Use grep with a basic regex pattern to find lines starting with a specific letter.
Print the matching lines to the terminal.
💡 Why This Matters
🌍 Real World
Searching text files for specific patterns is common in system administration, log analysis, and data processing.
💼 Career
Knowing how to use <code>grep</code> with regex helps automate searching tasks and quickly find needed information in files.
Progress0 / 4 steps
1
Create the text file items.txt
Create a text file called items.txt with these exact lines, each on its own line:
apple
banana
carrot
apricot
blueberry
Bash Scripting
Need a hint?

Use cat with a here-document or any text editor to create items.txt with the exact lines.

2
Set a variable for the filename
Create a variable called filename and set it to the string items.txt.
Bash Scripting
Need a hint?

Use filename="items.txt" to create the variable.

3
Use grep with basic regex to find lines starting with 'a'
Use grep with the pattern ^a on the file stored in the variable filename to find lines that start with the letter a. Write the command using the variable $filename.
Bash Scripting
Need a hint?

The pattern ^a means lines starting with 'a'. Use grep '^a' "$filename".

4
Print the matching lines
Run the command to print the lines from items.txt that start with a. The output should be exactly:
apple
apricot
Bash Scripting
Need a hint?

Just run the grep command from the previous step to see the matching lines.