0
0
Bash Scriptingscripting~10 mins

sed for substitution in scripts in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - sed for substitution in scripts
Start script
Read input text
Apply sed substitution
Replace matching text
Output modified text
End script
The script reads input text, applies sed substitution to replace matching text, then outputs the modified text.
Execution Sample
Bash Scripting
echo "Hello World" | sed 's/World/Bash/'
This command replaces 'World' with 'Bash' in the input text and outputs the result.
Execution Table
StepInput Textsed CommandSubstitution ResultOutput
1"Hello World"s/World/Bash/Hello BashHello Bash
2"No more input"No substitution neededNo more inputNo more input
3"End of input"No substitution neededEnd of inputEnd of input
💡 All input lines processed, script ends
Variable Tracker
VariableStartAfter Step 1After Step 2Final
input_text"Hello World""Hello Bash""No more input""End of input"
output_text"Hello Bash""No more input""End of input"
Key Moments - 3 Insights
Why does sed only replace the first match on a line by default?
By default, sed's 's/pattern/replacement/' replaces only the first occurrence per line, as shown in execution_table step 1 where only 'World' is replaced once.
How to replace all occurrences of a pattern in a line?
Add the 'g' flag like 's/pattern/replacement/g' to replace all matches on a line, which is not shown here but changes the substitution behavior.
What happens if the pattern is not found in the input?
sed outputs the line unchanged, as seen in execution_table steps 2 and 3 where no substitution occurs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 1?
A"Hello Bash"
B"Hello World"
C"Bash World"
D"Hello"
💡 Hint
Check the 'Output' column in execution_table row for step 1
At which step does sed not perform any substitution?
AStep 1
BStep 2
CBoth Step 2 and Step 3
DStep 3
💡 Hint
Look at the 'Substitution Result' column in execution_table for steps 2 and 3
If we change the sed command to 's/World/Bash/g', how would the output change for input 'World World'?
A"Bash World"
B"Bash Bash"
C"World Bash"
D"World World"
💡 Hint
The 'g' flag replaces all occurrences on the line, not just the first
Concept Snapshot
sed substitution syntax: s/pattern/replacement/
By default, replaces first match per line
Add 'g' flag to replace all matches
Used in scripts to modify text streams
Outputs modified text line by line
Full Transcript
This example shows how sed is used in scripts to substitute text. The script reads input lines, applies the substitution command s/World/Bash/, which replaces the first occurrence of 'World' with 'Bash' on each line, then outputs the result. If the pattern is not found, the line is output unchanged. Adding the 'g' flag to the command replaces all occurrences on a line. The execution table traces each input line, the sed command applied, the substitution result, and the output. The variable tracker shows how input and output text change step by step. Key moments clarify common confusions about sed's default behavior and flags. The visual quiz tests understanding of the substitution results and effects of flags.