Bird
Raised Fist0
Linux CLIscripting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the sed command sed 's/cat/dog/' file.txt do?
easy
A. Deletes all lines containing 'cat' in file.txt
B. Replaces the first occurrence of 'cat' with 'dog' on each line of file.txt
C. Prints only lines containing 'dog' from file.txt
D. Appends 'dog' at the end of each line containing 'cat' in file.txt

Solution

  1. Step 1: Understand the substitution command

    The s/pattern/replacement/ syntax in sed replaces the first match of 'pattern' with 'replacement' on each line.
  2. Step 2: Apply to the given command

    The command replaces the first occurrence of 'cat' with 'dog' on every line of file.txt.
  3. Final Answer:

    Replaces the first occurrence of 'cat' with 'dog' on each line of file.txt -> Option B
  4. Quick Check:

    sed substitution = replace first match [OK]
Hint: Remember: s/pattern/replacement/ changes first match per line [OK]
Common Mistakes:
  • Thinking it deletes lines instead of substituting
  • Assuming it replaces all occurrences without 'g' flag
  • Confusing printing lines with substitution
2. Which of the following is the correct syntax to delete all lines containing the word 'error' using sed?
easy
A. sed '/error/d' filename
B. sed 'd/error/' filename
C. sed 's/error/d/' filename
D. sed 'delete /error/' filename

Solution

  1. Step 1: Identify the delete command syntax in sed

    To delete lines matching a pattern, the syntax is sed '/pattern/d'.
  2. Step 2: Match the correct option

    sed '/error/d' filename uses sed '/error/d', which deletes lines containing 'error'. Other options have incorrect syntax.
  3. Final Answer:

    sed '/error/d' filename -> Option A
  4. Quick Check:

    Delete lines with pattern = /pattern/d [OK]
Hint: Use '/pattern/d' to delete matching lines in sed [OK]
Common Mistakes:
  • Placing 'd' before the pattern
  • Using substitution syntax instead of delete
  • Writing 'delete' instead of 'd'
3. What is the output of the command echo 'apple banana apple' | sed 's/apple/orange/g'?
medium
A. orange banana orange
B. orange banana apple
C. apple banana apple
D. apple banana orange

Solution

  1. 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.
  2. Step 2: Apply substitution to the input string

    All 'apple' words are replaced by 'orange', so 'apple banana apple' becomes 'orange banana orange'.
  3. Final Answer:

    orange banana orange -> Option A
  4. Quick Check:

    Substitute all matches with 'g' flag = replaced all [OK]
Hint: Add 'g' to replace all matches, not just first [OK]
Common Mistakes:
  • Forgetting the 'g' flag replaces only first occurrence
  • Confusing output with original input
  • Misreading the pipe input
4. Identify the error in this sed command: sed 's/hello/goodbye' file.txt
medium
A. No error; command is correct
B. Incorrect command; 's' cannot be used with text files
C. The pattern 'hello' is invalid
D. Missing delimiter after replacement text

Solution

  1. Step 1: Check the substitution syntax

    The substitution command requires delimiters: s/pattern/replacement/. The final '/' is missing here.
  2. Step 2: Identify the missing delimiter

    The command ends with 's/hello/goodbye' missing the closing '/' delimiter after 'goodbye'.
  3. Final Answer:

    Missing delimiter after replacement text -> Option D
  4. Quick Check:

    Substitution needs closing '/' delimiter [OK]
Hint: Check that substitution ends with '/' delimiter [OK]
Common Mistakes:
  • Omitting the last '/' in substitution
  • Assuming 's' can't be used on files
  • Thinking pattern must be a regex error
5. You want to replace the word 'cat' with 'dog' only on lines 2 to 4 in a file named pets.txt. Which sed command achieves this?
hard
A. sed 's/cat/dog/ 2,4' pets.txt
B. sed '/2,4 s/cat/dog/' pets.txt
C. sed '2,4s/cat/dog/' pets.txt
D. sed 's/cat/dog/ 2-4' pets.txt

Solution

  1. Step 1: Understand address range in sed

    To apply a command only on specific lines, use start,endcommand syntax, e.g., 2,4s/pattern/replacement/.
  2. Step 2: Match the correct syntax

    sed '2,4s/cat/dog/' pets.txt correctly uses 2,4s/cat/dog/ to replace 'cat' with 'dog' on lines 2 to 4.
  3. Final Answer:

    sed '2,4s/cat/dog/' pets.txt -> Option C
  4. Quick Check:

    Use line range before command to limit sed action [OK]
Hint: Use 'start,end' before 's' to limit substitution lines [OK]
Common Mistakes:
  • Placing line numbers after substitution command
  • Using incorrect range syntax like '2-4'
  • Adding slashes around line numbers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard