sed for substitution in scripts in Bash Scripting - Time & Space Complexity
When using sed for substitution in scripts, it is important to understand how the time it takes grows as the input size increases.
We want to know how the execution time changes when the input file or text gets bigger.
Analyze the time complexity of the following sed substitution command in a script.
sed 's/old/new/g' input.txt > output.txt
This command replaces all occurrences of the word "old" with "new" in the file input.txt and writes the result to output.txt.
Identify the loops, recursion, array traversals that repeat.
- Primary operation:
sedreads each line of the input file one by one. - How many times: It processes every line and checks each character for matches to replace.
As the input file grows, sed must check more lines and characters.
| Input Size (n lines) | Approx. Operations |
|---|---|
| 10 | Checks all characters in 10 lines |
| 100 | Checks all characters in 100 lines |
| 1000 | Checks all characters in 1000 lines |
Pattern observation: The work grows roughly in direct proportion to the number of lines and characters in the input.
Time Complexity: O(n)
This means the time to complete the substitution grows linearly with the size of the input file.
[X] Wrong: "The substitution runs instantly no matter the file size."
[OK] Correct: The command must read and check every character, so bigger files take more time.
Understanding how tools like sed scale with input size helps you write efficient scripts and explain your choices clearly in interviews.
What if we used sed to substitute only the first occurrence per line instead of all occurrences? How would the time complexity change?
Practice
sed command sed 's/cat/dog/' file.txt do?Solution
Step 1: Understand the sed substitution syntax
The commandsed 's/cat/dog/' file.txtusess/old/new/which replaces only the first match per line.Step 2: Apply to the file content
Each line in file.txt will have its first 'cat' replaced by 'dog', other occurrences remain unchanged.Final Answer:
Replaces the first occurrence of 'cat' with 'dog' on each line of file.txt -> Option CQuick Check:
sed s/old/new/ replaces first match per line [OK]
- Thinking it replaces all matches without 'g'
- Confusing substitution with deletion
- Assuming it prints only matching lines
Solution
Step 1: Identify the global flag usage
To replace all occurrences on each line, the 'g' flag is needed after the substitution pattern.Step 2: Check each option
sed 's/apple/orange/g' file.txt usess/apple/orange/gwhich correctly replaces all 'apple' with 'orange'. sed 's/apple/orange/' file.txt replaces only first occurrence. sed 'replace apple with orange' file.txt is invalid syntax. sed 's/apple/orange//g' file.txt has extra slashes causing syntax error.Final Answer:
sed 's/apple/orange/g' file.txt -> Option DQuick Check:
Use 'g' flag for all matches [OK]
- Omitting the 'g' flag to replace all matches
- Adding extra slashes causing syntax errors
- Using incorrect command words like 'replace'
apple apple apple?
sed 's/apple/orange/' file.txt
Solution
Step 1: Understand sed substitution without 'g'
Without the 'g' flag, sed replaces only the first occurrence of 'apple' per line.Step 2: Apply substitution to the input line
The first 'apple' becomes 'orange', the rest remain unchanged, resulting in 'orange apple apple'.Final Answer:
orange apple apple -> Option BQuick Check:
First match replaced only without 'g' [OK]
- Assuming all occurrences change without 'g'
- Confusing order of words after substitution
- Thinking sed changes input file by default
sed 's/foo/bar/' file.txt. But only the first 'foo' per line is replaced. What is the fix?Solution
Step 1: Identify missing global flag
The command lacks the 'g' flag, so only first occurrence per line is replaced.Step 2: Add the 'g' flag to fix
Adding 'g' after the substitution pattern makes sed replace all occurrences per line.Final Answer:
Add the 'g' flag: sed 's/foo/bar/g' file.txt -> Option AQuick Check:
Global flag 'g' replaces all matches [OK]
- Thinking quotes affect substitution behavior
- Adding spaces inside substitution pattern
- Confusing in-place edit with substitution flags
Solution
Step 1: Understand conditional substitution in sed
To apply substitution only on lines matching a pattern, use/pattern/ s/old/new/flagssyntax.Step 2: Analyze options
sed '/log/ s/error/warning/g' file.txt correctly applies substitution only on lines containing 'log'. Options A and C have invalid syntax. sed 's/error/warning/g' file.txt | grep log replaces everywhere then filters lines, which does not limit substitution.Final Answer:
sed '/log/ s/error/warning/g' file.txt -> Option AQuick Check:
Use address before s/// to limit lines [OK]
- Placing substitution after pattern incorrectly
- Trying to use if statements inside sed
- Filtering lines after substitution instead of limiting it
Ready to prove it?
Pass the test and this pattern moves unknown
