0
0
Bash Scriptingscripting~20 mins

sed for substitution in scripts in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sed Substitution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this sed substitution command?
Given a file colors.txt containing:
red
blue
green
blue
What will be the output of this command?
sed 's/blue/yellow/' colors.txt
A
red
yellow
green
yellow
B
red
blue
green
blue
C
red
yellow
green
blue
D
red
yellow
yellow
yellow
Attempts:
2 left
💡 Hint
Remember, sed by default replaces only the first match per line.
💻 Command Output
intermediate
2:00remaining
What does this sed command output?
Given a file data.txt with content:
apple apple apple
banana apple
apple banana
What is the output of:
sed 's/apple/orange/g' data.txt
A
orange orange orange
banana orange
orange banana
B
orange apple apple
banana apple
apple banana
C
orange orange orange
banana apple
apple banana
D
apple apple apple
banana apple
apple banana
Attempts:
2 left
💡 Hint
The 'g' flag replaces all occurrences on each line.
🔧 Debug
advanced
2:00remaining
Why does this sed substitution fail to replace text?
Consider this command:
echo 'hello world' | sed 's/Hello/Hi/'

Why does it output hello world unchanged?
AThe command is missing the -i flag for in-place editing
Bsed is case-sensitive and 'Hello' does not match 'hello'
CThe pipe symbol '|' is used incorrectly
DThe substitution syntax is invalid
Attempts:
2 left
💡 Hint
Check the letter case in the pattern and input.
🚀 Application
advanced
2: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?
Ased 's/cat/dog/1' filename
Bsed 's/cat/dog/g2' filename
Csed 's/cat/dog/2' filename
Dsed 's/cat/dog/3' filename
Attempts:
2 left
💡 Hint
sed supports a number after the substitution to specify which occurrence to replace.
🧠 Conceptual
expert
3: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 four
What is the output of:
sed -E 's/(two)/[\1]/2g' file.txt
A
one [two] three [two] one
four [two] five [two] four
B
one two three [two] one
four [two] five [two] four
C
one two three two one
four two five two four
D
one two three [two] one
four two five [two] four
Attempts:
2 left
💡 Hint
The '2g' after the substitution means replace the second and subsequent occurrences per line.