0
0
Linux CLIscripting~20 mins

sed substitution in Linux CLI - 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
1:30remaining
Output of sed substitution with global flag
What is the output of this command when run on the input string apple apple apple?
Linux CLI
echo "apple apple apple" | sed 's/apple/orange/g'
A
orange apple apple
B
orange orange orange
C
apple orange apple
D
apple apple orange
Attempts:
2 left
💡 Hint
The g flag means replace all occurrences in each line.
💻 Command Output
intermediate
1:30remaining
Effect of sed substitution without global flag
What is the output of this command when run on the input string banana banana banana?
Linux CLI
echo "banana banana banana" | sed 's/banana/grape/'
A
banana banana grape
B
grape grape grape
C
grape banana banana
D
banana grape banana
Attempts:
2 left
💡 Hint
Without the g flag, only the first match is replaced.
📝 Syntax
advanced
2:00remaining
Identify the correct sed substitution syntax
Which option shows the correct syntax to replace all occurrences of cat with dog in a file named pets.txt?
Ased 's/cat/dog/g' pets.txt
Bsed s/cat/dog/g pets.txt
Csed "s/cat/dog/g" pets.txt
Dsed -s 'cat' 'dog' pets.txt
Attempts:
2 left
💡 Hint
The substitution command should be enclosed in single quotes with the s///g format.
💻 Command Output
advanced
1:30remaining
Output of sed substitution with special characters
What is the output of this command?
Linux CLI
echo "path/to/file" | sed 's/\//-/g'
A
path/to/file
B
path-to/file
C
path\to\file
D
path-to-file
Attempts:
2 left
💡 Hint
The slash character is escaped to be replaced by a dash globally.
💻 Command Output
expert
2:30remaining
Result of sed substitution with capture groups and backreferences
What is the output of this command?
Linux CLI
echo "2024-06-15" | sed 's/\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)/\3\/\2\/\1/'
A
15/06/2024
B
06/15/2024
C
2024/06/15
D
15-06-2024
Attempts:
2 left
💡 Hint
Backreferences \1, \2, \3 refer to year, month, day respectively.