0
0
Linux CLIscripting~20 mins

sed (stream editor) basics in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sed Stream Editor 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 command?
Given a file data.txt with content:
apple
banana
cherry
banana
apple

What is the output of:
sed 's/banana/orange/' data.txt
Linux CLI
sed 's/banana/orange/' data.txt
A
apple
orange
cherry
orange
apple
B
apple
orange
cherry
banana
apple
C
apple
banana
cherry
banana
apple
D
apple
orange
cherry
cherry
apple
Attempts:
2 left
💡 Hint
Remember, sed replaces only the first match per line by default.
💻 Command Output
intermediate
2:00remaining
What does this sed command output?
Given file.txt content:
cat
bat
rat
mat

What is the output of:
sed -n '/at/p' file.txt
Linux CLI
sed -n '/at/p' file.txt
A
tam
tar
tab
tac
B
cat
bat
rat
C
at
bat
rat
mat
D
cat
bat
rat
mat
Attempts:
2 left
💡 Hint
The -n option suppresses automatic printing; /pattern/p prints matching lines.
📝 Syntax
advanced
2:00remaining
Which sed command correctly deletes lines 2 to 4?
You want to delete lines 2, 3, and 4 from a file using sed. Which command is correct?
Ased '2,4d' filename
Bsed '2-4d' filename
Csed '2;4d' filename
Dsed '2d4d' filename
Attempts:
2 left
💡 Hint
Ranges in sed use a comma, not a dash or semicolon.
🔧 Debug
advanced
2:00remaining
Why does this sed command fail?
You run:
sed 's/\(foo\)\(bar\)/\2\1/' file.txt

but it outputs the original text unchanged. Why?
Linux CLI
sed 's/\(foo\)\(bar\)/\2\1/' file.txt
AThe command is correct; the file has no 'foobar' to match.
BThe backreferences \1 and \2 are not recognized; sed needs single backslashes.
CThe pattern does not match because the parentheses are escaped incorrectly.
DThe replacement syntax is wrong; it should use $1 and $2 instead of \1 and \2.
Attempts:
2 left
💡 Hint
Check if the input file contains the pattern 'foobar'.
🚀 Application
expert
3:00remaining
How to swap two words on each line using sed?
You want to swap the first two words on each line of a file using sed. Which command achieves this?
Ased 's/\b(\w+) (\w+)\b/\2 \1/' filename
Bsed 's/(\S+) (\S+)/\2 \1/' filename
Csed 's/\([^ ]*\) \([^ ]*\)/\2 \1/' filename
Dsed 's/\(\w+\) \(\w+\)/\2 \1/' filename
Attempts:
2 left
💡 Hint
sed uses basic regular expressions; \w and \b are not supported by default.