0
0
Bash Scriptingscripting~15 mins

Anchors (^, $) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Anchors (^, $) in Bash Scripting
📖 Scenario: You work in a small company where you need to filter log entries. The logs contain many lines, but you want to find only those lines that start or end with specific words.
🎯 Goal: You will write a bash script that uses anchors ^ and $ in regular expressions to find lines starting with a word and lines ending with a word from a list of log entries.
📋 What You'll Learn
Create a variable with multiple log lines
Create a variable with a word to match at the start of lines
Use grep with the anchor ^ to find lines starting with the word
Use grep with the anchor $ to find lines ending with the word
Print the filtered results
💡 Why This Matters
🌍 Real World
Filtering logs or text files to find lines that start or end with specific words is common in system administration and data processing.
💼 Career
Understanding anchors in regular expressions helps automate text filtering tasks, which is valuable for roles in DevOps, system administration, and scripting.
Progress0 / 4 steps
1
Create a variable with log lines
Create a variable called logs that contains these exact lines separated by newlines: error: file not found, warning: low disk space, info: update completed, error: access denied, info: user logged in
Bash Scripting
Need a hint?

Use double quotes and \n to separate lines inside the string.

2
Create a variable with the start word to match
Create a variable called start_word and set it to the exact string error
Bash Scripting
Need a hint?

Use double quotes to assign the string.

3
Use grep with ^ anchor to find lines starting with the word
Use echo and grep with the pattern ^$start_word to filter lines from logs that start with the word stored in start_word. Save the result in a variable called start_matches
Bash Scripting
Need a hint?

Use double quotes around the pattern and variable to expand it correctly.

4
Use grep with $ anchor to find lines ending with a word and print results
Create a variable called end_word and set it to completed. Then use echo and grep with the pattern $end_word$ to find lines in logs that end with end_word. Save the result in end_matches. Finally, print start_matches and end_matches with echo.
Bash Scripting
Need a hint?

Remember to use double quotes and variable expansion in the grep pattern. Use echo to print the results.