Anchors (^, $) in Bash Scripting - Time & Space Complexity
When using anchors like ^ and $ in bash scripting, we want to know how the script's speed changes as input grows.
We ask: How does matching lines with anchors affect the time it takes?
Analyze the time complexity of the following code snippet.
#!/bin/bash
pattern='^hello$'
while IFS= read -r line; do
if [[ $line =~ $pattern ]]; then
echo "Match: $line"
fi
done < input.txt
This script reads each line from a file and checks if it exactly matches the word "hello" using anchors.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each line and testing it against the anchored pattern.
- How many times: Once for every line in the input file.
Each new line adds one more pattern check, so the work grows steadily with the number of lines.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 pattern checks |
| 100 | 100 pattern checks |
| 1000 | 1000 pattern checks |
Pattern observation: The time grows directly with the number of lines; double the lines, double the checks.
Time Complexity: O(n)
This means the script takes longer in a straight line as the input file gets bigger.
[X] Wrong: "Anchors make the script run faster because they limit matches."
[OK] Correct: Anchors only change what matches, not how many lines are checked. The script still tests every line.
Understanding how pattern matching scales helps you write scripts that stay fast even with big files. This skill shows you can think about efficiency clearly.
"What if we changed the pattern to match anywhere in the line without anchors? How would the time complexity change?"
Practice
^ represent in a bash regular expression?Solution
Step 1: Understand the role of anchors in regex
Anchors are special characters that match positions in text, not actual characters.Step 2: Identify the meaning of
The^^anchor matches the start of a line in bash regex.Final Answer:
It matches the start of a line. -> Option DQuick Check:
^ means start of line [OK]
- Confusing ^ with end of line anchor
- Thinking ^ matches a character instead of position
- Mixing ^ with other regex symbols like * or .
done?Solution
Step 1: Understand the meaning of
The$anchor$anchor matches the end of a line in regex.Step 2: Match lines ending with 'done'
Usingdone$matches lines where 'done' is at the end.Final Answer:
grep 'done$' file.txt -> Option AQuick Check:
Use $ to match line end [OK]
- Using ^ after text instead of $
- Using ^done to match line end
- Confusing ^ and $ anchors
file.txt contains the lines:apple banana apple pie pineapple apple
grep '^apple$' file.txtSolution
Step 1: Understand the regex
This matches lines that start and end exactly with 'apple' and nothing else.^apple$Step 2: Check each line in file.txt
Lines 'apple' and 'apple' match exactly. 'apple pie' and 'pineapple' do not match because of extra text.Final Answer:
apple apple -> Option CQuick Check:
^ and $ match exact whole line [OK]
- Assuming partial matches with anchors
- Ignoring that $ matches end of line
- Confusing lines with extra words as matches
grep 'Error^' logfile.txtBut it returns no results. What is the problem?
Solution
Step 1: Analyze the position of the anchor ^
The^anchor must be at the start of the pattern to match line start.Step 2: Correct the pattern placement
Use^Errorto match lines starting with 'Error'. Placing ^ after 'Error' is invalid.Final Answer:
The anchor ^ should be at the start, not after 'Error'. -> Option BQuick Check:
^ must be at pattern start [OK]
- Putting ^ after text instead of before
- Assuming grep ignores anchors
- Using wrong quotes causing shell issues
data.txt that start with a digit and end with a period. Which grep pattern correctly uses anchors to do this?Solution
Step 1: Match line start with a digit
The pattern^[0-9]matches lines starting with any digit.Step 2: Match line end with a literal period
Use\.$to match a literal '.' at the end. The dot must be escaped to avoid matching any character.Step 3: Combine pattern correctly
^[0-9].*\.$matches lines starting with digit, any characters, ending with a period.Final Answer:
grep '^[0-9].*\.$' data.txt -> Option AQuick Check:
^ start, $ end, escape . for literal dot [OK]
- Not escaping dot, matching any char instead of period
- Using \d which grep may not support by default
- Missing $ anchor at line end
Ready to prove it?
Pass the test and this pattern moves unknown
