sed substitution in Linux CLI - Time & Space Complexity
We want to understand how the time taken by a sed substitution command changes as the input size grows.
Specifically, how does sed handle replacing text in larger files?
Analyze the time complexity of the following sed command.
sed 's/apple/orange/g' input.txt > output.txt
This command replaces every occurrence of "apple" with "orange" in the file input.txt and writes the result to output.txt.
Look for repeated actions inside the command.
- Primary operation: sed reads each line of the file and scans for the pattern "apple" to replace.
- How many times: It processes every line once, and within each line, it checks every character to find matches.
As the file gets bigger, sed must read more lines and check more 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 file size.
Time Complexity: O(n)
This means the time sed takes grows linearly with the size of the input file.
[X] Wrong: "sed substitution runs instantly no matter the file size because it's just a simple replace."
[OK] Correct: sed must read and check every part of the file, so bigger files take more time.
Understanding how tools like sed scale with input size helps you write efficient scripts and shows you think about performance in real tasks.
"What if we used sed to replace 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/pattern/replacement/which replaces the first match per line.Step 2: Apply the command to the file
It replaces only the first 'cat' found on each line with 'dog', not all occurrences or lines.Final Answer:
Replaces the first occurrence of 'cat' with 'dog' on each line of file.txt -> Option AQuick Check:
sed s/pattern/replacement/ replaces first match per line [OK]
- Thinking all matches are replaced without 'g'
- Confusing substitution with deletion
- Assuming it prints only matching lines
sed?Solution
Step 1: Identify the flag for global replacement
The 'g' flag after the substitution pattern tells sed to replace all occurrences on each line.Step 2: Check syntax correctness
sed 's/apple/orange/g' file.txt usessed 's/apple/orange/g'which is the correct syntax without extra characters.Final Answer:
sed 's/apple/orange/g' file.txt -> Option BQuick Check:
'g' flag means replace all matches per line [OK]
- Omitting the 'g' flag for all replacements
- Adding extra semicolons inside quotes
- Using 'gp' which is invalid here
input.txt contains:apple apple banana banana apple apple
sed 's/apple/orange/' input.txt?Solution
Step 1: Understand sed substitution without 'g'
Without 'g', only the first 'apple' on each line is replaced by 'orange'.Step 2: Apply substitution line by line
Line 1: 'apple apple banana' becomes 'orange apple banana'. Line 2: 'banana apple apple' becomes 'banana orange apple'.Final Answer:
orange apple banana banana orange apple -> Option CQuick Check:
First match per line replaced only [OK]
- Replacing all 'apple' without 'g'
- Replacing none due to misunderstanding
- Confusing line order or content
sed 's/hello/world/g file.txt'Solution
Step 1: Check the quotes in the command
The command has an opening single quote before s/hello/world/g but no closing quote after 'g'.Step 2: Confirm correct placement of quotes and arguments
The file name should be outside the quotes. The missing closing quote causes syntax error.Final Answer:
Missing closing quote after 'g' -> Option DQuick Check:
Quotes must enclose entire sed expression [OK]
- Placing file name inside quotes
- Forgetting closing quote
- Confusing delimiter with quotes
data.txt. Which command achieves this?Solution
Step 1: Understand address restriction in sed
Using/baz/ s/foo/bar/gapplies substitution only on lines matching 'baz'.Step 2: Verify command correctness
sed '/baz/ s/foo/bar/g' data.txt correctly restricts substitution to lines with 'baz' and replaces all 'foo' with 'bar'.Final Answer:
sed '/baz/ s/foo/bar/g' data.txt -> Option AQuick Check:
Address before command limits lines affected [OK]
- Placing substitution after file name
- Using grep after sed instead of address
- Mixing patterns in wrong order
Ready to prove it?
Pass the test and this pattern moves unknown
