0
0
Bash Scriptingscripting~15 mins

sed for substitution in scripts in Bash Scripting - Deep Dive

Choose your learning style9 modes available
Overview - sed for substitution in scripts
What is it?
sed is a command-line tool used to find and replace text in files or input streams. It reads text line by line, applies instructions like substitution, and outputs the changed text. In scripts, sed helps automate editing tasks without opening files manually. It is especially useful for quick, repeatable text changes.
Why it matters
Without sed, changing text in many files or streams would require manual editing or complex programming. sed makes it easy to automate text replacements, saving time and reducing errors. This is crucial in scripting where repetitive text edits are common, such as updating configuration files or cleaning data. Without sed, automation would be slower and more error-prone.
Where it fits
Learners should know basic shell commands and text file handling before using sed. After mastering sed substitution, they can explore more advanced text processing tools like awk or Perl, or learn how to combine sed with other commands in complex scripts.
Mental Model
Core Idea
sed reads text line by line, applies substitution rules, and outputs the changed text automatically.
Think of it like...
sed is like a smart find-and-replace assistant who reads a letter line by line and swaps words exactly as you tell it, without you having to open the letter yourself.
Input Text ──▶ [sed substitution command] ──▶ Output Text

┌─────────────┐       ┌─────────────────────────────┐       ┌─────────────┐
│ Original    │──────▶│ sed reads each line, applies │──────▶│ Modified    │
│ text lines  │       │ substitution rules           │       │ text lines  │
└─────────────┘       └─────────────────────────────┘       └─────────────┘
Build-Up - 7 Steps
1
FoundationBasic sed substitution syntax
🤔
Concept: Learn the simplest form of sed substitution command.
The basic sed substitution command looks like this: sed 's/old/new/' filename This means: find the first occurrence of 'old' in each line and replace it with 'new'. The 's' stands for substitute. By default, only the first match per line changes.
Result
Lines in the file where 'old' appears will show 'new' instead for the first match per line.
Understanding the basic syntax is the foundation for all sed text replacements.
2
FoundationUsing sed with input streams
🤔
Concept: sed can read from standard input, not just files.
You can pipe text into sed instead of giving a filename: echo 'hello old world' | sed 's/old/new/' This replaces 'old' with 'new' in the echoed text and prints the result.
Result
hello new world
Knowing sed works with streams lets you use it flexibly in scripts and pipelines.
3
IntermediateReplacing all occurrences per line
🤔Before reading on: do you think sed replaces all matches in a line by default or only the first? Commit to your answer.
Concept: Add the 'g' flag to replace every match in each line, not just the first.
By default, sed replaces only the first match per line. To replace all matches, add 'g' after the last slash: sed 's/old/new/g' filename This changes every 'old' in each line to 'new'.
Result
All occurrences of 'old' in each line are replaced with 'new'.
Knowing the 'g' flag prevents partial replacements and ensures complete text changes.
4
IntermediateUsing different delimiters in substitution
🤔Before reading on: do you think the delimiter in sed substitution must always be a slash '/'? Commit to your answer.
Concept: You can use other characters as delimiters to avoid confusion with slashes in text.
If the text contains slashes, using '/' as delimiter is confusing. sed allows other delimiters like '#': sed 's#path/old#path/new#g' filename This replaces 'path/old' with 'path/new' without escaping slashes.
Result
Text with slashes replaced correctly without extra escaping.
Changing delimiters simplifies substitutions involving special characters.
5
IntermediateIn-place file editing with sed
🤔
Concept: sed can modify files directly instead of just printing output.
Use the '-i' option to edit files in place: sed -i 's/old/new/g' filename This changes the file content directly, replacing all 'old' with 'new'.
Result
The file is updated with substitutions applied.
In-place editing is powerful for automation but requires care to avoid unwanted changes.
6
AdvancedUsing sed with capture groups and backreferences
🤔Before reading on: do you think sed can remember parts of matched text to reuse in replacement? Commit to your answer.
Concept: sed supports capturing parts of matched text and reusing them in the replacement.
Parentheses \( and \) capture parts of the match. Use \1, \2, etc. to reuse them: sed 's/\(old\) text/\1 replaced/' filename This replaces 'old text' with 'old replaced', keeping 'old' from the original.
Result
Text with parts reused in the replacement, preserving matched content.
Capture groups let you do complex replacements that keep some original text.
7
ExpertCombining multiple sed substitutions in scripts
🤔Before reading on: do you think multiple sed commands can be combined in one call or must be separate? Commit to your answer.
Concept: You can combine many substitutions in one sed command or script for efficiency.
Use -e to add multiple commands: sed -e 's/old/new/g' -e 's/foo/bar/g' filename Or write commands in a file and run: sed -f commands.sed filename This runs all substitutions in one pass.
Result
All specified substitutions apply in order, improving script clarity and speed.
Combining commands reduces overhead and organizes complex text edits cleanly.
Under the Hood
sed reads input line by line into memory, applies the substitution commands using pattern matching and replacement rules, then outputs the modified line. It uses regular expressions to find matches and can keep track of captured groups for backreferences. When editing files in place, sed creates a temporary file with changes and replaces the original file atomically.
Why designed this way?
sed was designed in the 1970s to automate text editing on streams efficiently with minimal memory. Line-by-line processing fits well with Unix pipelines and avoids loading entire files. The substitution syntax is compact and flexible, balancing power and simplicity. Alternatives like full scripting languages were heavier and slower at the time.
Input Stream
   │
   ▼
┌───────────────┐
│ sed Processor │
│ ┌───────────┐ │
│ │ Read line │ │
│ └───────────┘ │
│ ┌───────────┐ │
│ │ Match &   │ │
│ │ Substitute│ │
│ └───────────┘ │
│ ┌───────────┐ │
│ │ Output    │ │
│ └───────────┘ │
└───────────────┘
   │
   ▼
Output Stream or File
Myth Busters - 4 Common Misconceptions
Quick: Does sed replace all matches in a line by default? Commit to yes or no.
Common Belief:sed replaces all matches in each line by default.
Tap to reveal reality
Reality:sed replaces only the first match per line unless the 'g' flag is added.
Why it matters:Without the 'g' flag, some matches remain unchanged, causing incomplete substitutions and bugs.
Quick: Can you use any character as delimiter in sed substitution? Commit to yes or no.
Common Belief:The delimiter in sed substitution must always be a slash '/'.
Tap to reveal reality
Reality:You can use other delimiters like '#', '@', or '|' to avoid escaping slashes in text.
Why it matters:Using only '/' forces complex escaping, making scripts harder to read and maintain.
Quick: Does sed edit files directly without options? Commit to yes or no.
Common Belief:Running sed with substitution edits the file directly.
Tap to reveal reality
Reality:sed outputs changes to standard output by default; the file is unchanged unless '-i' is used.
Why it matters:Assuming files change without '-i' leads to confusion and failed automation.
Quick: Can sed reuse parts of matched text in replacements? Commit to yes or no.
Common Belief:sed cannot remember parts of the match for reuse in replacement.
Tap to reveal reality
Reality:sed supports capture groups and backreferences to reuse matched text parts.
Why it matters:Not knowing this limits the complexity of text transformations you can automate.
Expert Zone
1
sed's in-place editing with '-i' can behave differently across Unix variants; some require a backup suffix, others do not.
2
When using capture groups, parentheses must be escaped '\( \)' in basic sed, but unescaped in extended sed (-E).
3
sed processes commands sequentially; order matters when combining multiple substitutions or commands.
When NOT to use
sed is not ideal for multi-line or context-aware edits; tools like awk, Perl, or Python scripts handle complex parsing better. For very large files requiring random access, specialized editors or programming languages are preferable.
Production Patterns
In production scripts, sed is often combined with shell loops and conditionals to batch-edit config files. It is used in CI/CD pipelines for dynamic version or environment updates. Experts write sed scripts with comments and use separate command files for maintainability.
Connections
Regular Expressions
sed substitution uses regular expressions to find text patterns.
Understanding regex deeply improves sed command precision and power.
Unix Pipelines
sed is commonly used as a filter in pipelines to transform streamed text.
Knowing how pipelines work helps you chain sed with other commands for complex automation.
Text Editing Automation
sed substitution automates manual text editing tasks.
Recognizing sed as an automation tool connects scripting with broader automation disciplines like RPA or macro scripting.
Common Pitfalls
#1Expecting sed to replace all matches without the 'g' flag.
Wrong approach:sed 's/old/new/' file.txt
Correct approach:sed 's/old/new/g' file.txt
Root cause:Not knowing that sed replaces only the first match per line by default.
#2Using '/' delimiter when the pattern contains slashes, causing confusing escapes.
Wrong approach:sed 's/path/old/path/new/' file.txt
Correct approach:sed 's#path/old#path/new#' file.txt
Root cause:Assuming '/' is the only delimiter and not using alternatives to simplify patterns.
#3Running sed substitution without '-i' expecting file changes.
Wrong approach:sed 's/old/new/g' file.txt
Correct approach:sed -i 's/old/new/g' file.txt
Root cause:Misunderstanding that sed outputs to stdout by default and does not edit files in place.
Key Takeaways
sed is a powerful tool for automating text substitutions in scripts by reading input line by line and applying rules.
By default, sed replaces only the first match per line; adding the 'g' flag replaces all matches.
You can use different delimiters in sed substitution to simplify patterns containing slashes.
In-place editing with '-i' modifies files directly but requires caution to avoid unintended changes.
Capture groups and backreferences let you reuse parts of matched text for complex replacements.