0
0
Linux CLIscripting~10 mins

sed (stream editor) basics in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - sed (stream editor) basics
Start with input text
sed reads line
Apply sed command
Output modified line
Repeat for next line
End of input?
Nosed reads next line
Yes
Finish
sed reads input line by line, applies the editing command, outputs the result, and repeats until all lines are processed.
Execution Sample
Linux CLI
echo "apple\nbanana\ncherry" | sed 's/a/A/'
Replace the first 'a' with 'A' on each line of the input text.
Execution Table
StepInput Linesed Command AppliedOutput LineNotes
1apples/a/A/AppleFirst 'a' replaced with 'A'
2bananas/a/A/bAnanaOnly first 'a' replaced
3cherrys/a/A/cherryNo 'a' found, line unchanged
4End of input--sed finishes processing all lines
💡 All input lines processed, sed ends.
Variable Tracker
VariableStartAfter Line 1After Line 2After Line 3Final
Current LineapplebananacherryEndEnd
Output LineApplebAnanacherryAll lines output
Key Moments - 2 Insights
Why does sed only replace the first 'a' on each line, not all?
By default, sed's 's' command replaces only the first match per line, as shown in execution_table rows 1 and 2.
What happens if the pattern is not found in a line?
sed outputs the line unchanged, as seen in execution_table row 3 where 'cherry' has no 'a'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output line for the second input line?
Abanana
BBanana
CbAnana
DbAnAna
💡 Hint
Check execution_table row 2 under 'Output Line'
At which step does sed finish processing all lines?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look at the 'End of input' step in execution_table
If we add the 'g' flag to the sed command (s/a/A/g), how would the output for 'banana' change?
AbAnAnA
BbAnana
Cbanana
DBanana
💡 Hint
The 'g' flag replaces all matches per line, not just the first (see key_moments about default behavior)
Concept Snapshot
sed reads input line by line
Applies editing commands like substitution
's/pattern/replacement/' replaces first match per line
Add 'g' flag to replace all matches
Outputs modified lines in order
Stops after last input line
Full Transcript
sed is a tool that reads text line by line. For each line, it applies a command like substitution. For example, 's/a/A/' changes the first 'a' to 'A' on each line. sed then outputs the changed line. It repeats this until all lines are done. If a line has no match, it stays the same. By default, only the first match per line changes. Adding 'g' after the command makes all matches change. This process helps quickly edit text streams.