Bird
Raised Fist0
Linux CLIscripting~10 mins

sed substitution in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - sed substitution
Read input line
↓
Match pattern?
No→Print line unchanged
Yes↓
Replace matched text
↓
Print modified line
↓
Repeat for next line
↓
End
sed reads each line, checks if it matches the pattern, replaces matched text if yes, then prints the line.
Execution Sample
Linux CLI
echo 'apple banana apple' | sed 's/apple/orange/'
Replace first 'apple' with 'orange' in the input line.
Execution Table
StepInput LinePattern MatchActionOutput Line
1apple banana appleYes (apple found)Replace first 'apple' with 'orange'orange banana apple
2No more linesN/AEnd of inputN/A
💡 No more input lines to process, sed ends.
Variable Tracker
VariableStartAfter Step 1Final
input_lineapple banana appleapple banana appleNo more lines
pattern_matchedfalsetrueN/A
output_lineorange banana apple
Key Moments - 2 Insights
Why does sed only replace the first occurrence of 'apple'?
By default, sed's 's/pattern/replacement/' replaces only the first match per line, as shown in execution_table step 1.
What happens if the pattern is not found in the line?
sed prints the line unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output line after step 1?
Aapple banana apple
Bapple banana orange
Corange banana apple
Dorange orange apple
💡 Hint
Check the 'Output Line' column in execution_table row for step 1.
At which step does sed stop processing input lines?
AStep 1
BStep 2
CAfter Step 1
DNever stops
💡 Hint
Look at the 'Step' and 'Action' columns in execution_table for when input ends.
If we add 'g' flag to sed like 's/apple/orange/g', how would the output line change at step 1?
Aorange banana orange
Borange banana apple
Capple banana apple
Dorange orange orange
💡 Hint
The 'g' flag replaces all matches per line, not just the first, affecting output_line in variable_tracker.
Concept Snapshot
sed substitution syntax: s/pattern/replacement/ 
By default replaces first match per line.
Add 'g' flag to replace all matches.
Reads input line by line, applies substitution, prints result.
Useful for quick text changes in scripts or command line.
Full Transcript
sed substitution reads each input line, checks if the pattern matches. If yes, it replaces the first occurrence of the pattern with the replacement text and prints the modified line. If no match, it prints the line unchanged. This process repeats for all input lines until none remain. By default, only the first match per line is replaced unless the 'g' flag is added to replace all matches. This makes sed a powerful tool for quick text editing in command line scripts.

Practice

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

Solution

  1. Step 1: Understand the sed substitution syntax

    The command sed 's/cat/dog/' file.txt uses s/pattern/replacement/ which replaces the first match per line.
  2. Step 2: Apply the command to the file

    It replaces only the first 'cat' found on each line with 'dog', not all occurrences or lines.
  3. Final Answer:

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

    sed s/pattern/replacement/ replaces first match per line [OK]
Hint: No 'g' flag means only first match per line replaced [OK]
Common Mistakes:
  • Thinking all matches are replaced without 'g'
  • Confusing substitution with deletion
  • Assuming it prints only matching lines
2. Which of the following is the correct syntax to replace all occurrences of 'apple' with 'orange' using sed?
easy
A. sed 's/apple/orange/' file.txt
B. sed 's/apple/orange/g' file.txt
C. sed 's/apple/orange/g;' file.txt
D. sed 's/apple/orange/gp' file.txt

Solution

  1. Step 1: Identify the flag for global replacement

    The 'g' flag after the substitution pattern tells sed to replace all occurrences on each line.
  2. Step 2: Check syntax correctness

    sed 's/apple/orange/g' file.txt uses sed 's/apple/orange/g' which is the correct syntax without extra characters.
  3. Final Answer:

    sed 's/apple/orange/g' file.txt -> Option B
  4. Quick Check:

    'g' flag means replace all matches per line [OK]
Hint: Add 'g' after last slash to replace all matches [OK]
Common Mistakes:
  • Omitting the 'g' flag for all replacements
  • Adding extra semicolons inside quotes
  • Using 'gp' which is invalid here
3. What is the output of this command if input.txt contains:
apple apple banana
banana apple apple

sed 's/apple/orange/' input.txt?
medium
A. orange orange banana banana orange orange
B. orange apple banana banana apple apple
C. orange apple banana banana orange apple
D. apple apple banana banana apple apple

Solution

  1. Step 1: Understand sed substitution without 'g'

    Without 'g', only the first 'apple' on each line is replaced by 'orange'.
  2. Step 2: Apply substitution line by line

    Line 1: 'apple apple banana' becomes 'orange apple banana'. Line 2: 'banana apple apple' becomes 'banana orange apple'.
  3. Final Answer:

    orange apple banana banana orange apple -> Option C
  4. Quick Check:

    First match per line replaced only [OK]
Hint: No 'g' means only first match per line changes [OK]
Common Mistakes:
  • Replacing all 'apple' without 'g'
  • Replacing none due to misunderstanding
  • Confusing line order or content
4. Identify the error in this sed command: sed 's/hello/world/g file.txt'
medium
A. File name should come before the command
B. Incorrect delimiter used
C. Missing 'g' flag for global replacement
D. Missing closing quote after 'g'

Solution

  1. Step 1: Check the quotes in the command

    The command has an opening single quote before s/hello/world/g but no closing quote after 'g'.
  2. Step 2: Confirm correct placement of quotes and arguments

    The file name should be outside the quotes. The missing closing quote causes syntax error.
  3. Final Answer:

    Missing closing quote after 'g' -> Option D
  4. Quick Check:

    Quotes must enclose entire sed expression [OK]
Hint: Always close quotes around sed expression [OK]
Common Mistakes:
  • Placing file name inside quotes
  • Forgetting closing quote
  • Confusing delimiter with quotes
5. You want to replace all occurrences of 'foo' with 'bar' only on lines containing 'baz' in data.txt. Which command achieves this?
hard
A. sed '/baz/ s/foo/bar/g' data.txt
B. sed 's/foo/bar/g /baz/' data.txt
C. sed 's/foo/bar/g' data.txt | grep baz
D. sed '/foo/ s/baz/bar/g' data.txt

Solution

  1. Step 1: Understand address restriction in sed

    Using /baz/ s/foo/bar/g applies substitution only on lines matching 'baz'.
  2. Step 2: Verify command correctness

    sed '/baz/ s/foo/bar/g' data.txt correctly restricts substitution to lines with 'baz' and replaces all 'foo' with 'bar'.
  3. Final Answer:

    sed '/baz/ s/foo/bar/g' data.txt -> Option A
  4. Quick Check:

    Address before command limits lines affected [OK]
Hint: Use /pattern/ before s/// to limit lines [OK]
Common Mistakes:
  • Placing substitution after file name
  • Using grep after sed instead of address
  • Mixing patterns in wrong order
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