sed (stream editor) basics in Linux CLI - Time & Space Complexity
When using sed, it processes text line by line. Understanding how its work grows with input size helps us predict how long it will take on bigger files.
We want to know: how does the time to run sed change as the number of lines grows?
Analyze the time complexity of the following sed command.
sed 's/foo/bar/g' input.txt > output.txt
This command replaces every occurrence of "foo" with "bar" in each line of the file.
Look at what repeats as the command runs:
- Primary operation: Reading and processing each line one by one.
- How many times: Once for every line in the input file.
As the file gets bigger, sed works through more lines, doing the same replace on each.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 lines | 10 replace checks |
| 100 lines | 100 replace checks |
| 1000 lines | 1000 replace checks |
Pattern observation: The work grows directly with the number of lines. Double the lines, double the work.
Time Complexity: O(n)
This means the time sed takes grows in a straight line with the number of lines it processes.
[X] Wrong: "sed runs instantly no matter the file size because it's a simple command."
[OK] Correct: Even simple commands must read and check every line. More lines mean more work and more time.
Knowing how sed scales helps you write scripts that handle big files efficiently. It shows you understand how tools behave with growing data, a key skill in scripting.
"What if we changed the command to only replace on lines matching a pattern? How would the time complexity change?"
Practice
sed command sed 's/cat/dog/' file.txt do?Solution
Step 1: Understand the substitution command
Thes/pattern/replacement/syntax insedreplaces the first match of 'pattern' with 'replacement' on each line.Step 2: Apply to the given command
The command replaces the first occurrence of 'cat' with 'dog' on every line offile.txt.Final Answer:
Replaces the first occurrence of 'cat' with 'dog' on each line of file.txt -> Option BQuick Check:
sed substitution = replace first match [OK]
- Thinking it deletes lines instead of substituting
- Assuming it replaces all occurrences without 'g' flag
- Confusing printing lines with substitution
sed?Solution
Step 1: Identify the delete command syntax in sed
To delete lines matching a pattern, the syntax issed '/pattern/d'.Step 2: Match the correct option
sed '/error/d' filename usessed '/error/d', which deletes lines containing 'error'. Other options have incorrect syntax.Final Answer:
sed '/error/d' filename -> Option AQuick Check:
Delete lines with pattern = /pattern/d [OK]
- Placing 'd' before the pattern
- Using substitution syntax instead of delete
- Writing 'delete' instead of 'd'
echo 'apple banana apple' | sed 's/apple/orange/g'?Solution
Step 1: Understand the 'g' flag in substitution
The 'g' flag means replace all occurrences of the pattern on each line, not just the first.Step 2: Apply substitution to the input string
All 'apple' words are replaced by 'orange', so 'apple banana apple' becomes 'orange banana orange'.Final Answer:
orange banana orange -> Option AQuick Check:
Substitute all matches with 'g' flag = replaced all [OK]
- Forgetting the 'g' flag replaces only first occurrence
- Confusing output with original input
- Misreading the pipe input
sed command: sed 's/hello/goodbye' file.txtSolution
Step 1: Check the substitution syntax
The substitution command requires delimiters:s/pattern/replacement/. The final '/' is missing here.Step 2: Identify the missing delimiter
The command ends with's/hello/goodbye'missing the closing '/' delimiter after 'goodbye'.Final Answer:
Missing delimiter after replacement text -> Option DQuick Check:
Substitution needs closing '/' delimiter [OK]
- Omitting the last '/' in substitution
- Assuming 's' can't be used on files
- Thinking pattern must be a regex error
pets.txt. Which sed command achieves this?Solution
Step 1: Understand address range in sed
To apply a command only on specific lines, usestart,endcommandsyntax, e.g.,2,4s/pattern/replacement/.Step 2: Match the correct syntax
sed '2,4s/cat/dog/' pets.txt correctly uses2,4s/cat/dog/to replace 'cat' with 'dog' on lines 2 to 4.Final Answer:
sed '2,4s/cat/dog/' pets.txt -> Option CQuick Check:
Use line range before command to limit sed action [OK]
- Placing line numbers after substitution command
- Using incorrect range syntax like '2-4'
- Adding slashes around line numbers
Ready to prove it?
Pass the test and this pattern moves unknown
