Challenge - 5 Problems
Sed Substitution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this sed substitution command?
Given a file
colors.txt containing:red blue green blueWhat will be the output of this command?
sed 's/blue/yellow/' colors.txt
Attempts:
2 left
💡 Hint
Remember, sed by default replaces only the first match per line.
✗ Incorrect
The sed command replaces only the first occurrence of 'blue' on each line. 'blue' appears once on lines 2 and 4, so both are replaced with 'yellow'.
💻 Command Output
intermediate2:00remaining
What does this sed command output?
Given a file
data.txt with content:apple apple apple banana apple apple bananaWhat is the output of:
sed 's/apple/orange/g' data.txt
Attempts:
2 left
💡 Hint
The 'g' flag replaces all occurrences on each line.
✗ Incorrect
The 'g' flag in sed means global replacement on each line, so all 'apple' words are replaced with 'orange'.
🔧 Debug
advanced2:00remaining
Why does this sed substitution fail to replace text?
Consider this command:
Why does it output
echo 'hello world' | sed 's/Hello/Hi/'
Why does it output
hello world unchanged?Attempts:
2 left
💡 Hint
Check the letter case in the pattern and input.
✗ Incorrect
sed matches patterns exactly by default, so 'Hello' does not match 'hello' due to case difference.
🚀 Application
advanced2:00remaining
How to replace only the second occurrence of a word in each line using sed?
You want to replace only the second occurrence of 'cat' with 'dog' in each line of a file. Which sed command achieves this?
Attempts:
2 left
💡 Hint
sed supports a number after the substitution to specify which occurrence to replace.
✗ Incorrect
The number after the substitution command specifies which occurrence to replace. '2' means the second occurrence only.
🧠 Conceptual
expert3:00remaining
What is the output of this complex sed script?
Given a file
file.txt with content:one two three two one four two five two fourWhat is the output of:
sed -E 's/(two)/[\1]/2g' file.txt
Attempts:
2 left
💡 Hint
The '2g' after the substitution means replace the second and subsequent occurrences per line.
✗ Incorrect
When a number followed by 'g' is given after 's///', sed replaces that occurrence and subsequent ones per line. Here, only the second 'two' per line is replaced with [\1] (backreference to the match).