Insert or Append Text with Sed: Master Advanced Techniques
Master sed's insert and append commands to add text precisely at any line position with practical examples and best practices.

Need to add text at specific positions in files? The sed command makes inserting and appending text simple and precise. Whether you’re adding configuration lines, inserting headers, or appending data, mastering sed’s i
and a
commands will streamline your text editing workflow.
What Is sed?
sed (Stream Editor) is a powerful command-line utility for filtering and transforming text in Unix-like systems. It excels at automated text editing because it processes files line by line without requiring user interaction, making it perfect for scripts and batch operations.
Key advantages for text insertion and appending:
- Non-interactive: Works automatically without user input after the initial command
- Precise positioning: Insert or append text at exact line numbers or pattern matches
- Efficient processing: Handles large files quickly without loading everything into memory
- Scriptable: Perfect for automation and repetitive tasks
Why Use sed for Text Insertion?
- Speed: Process files rapidly, even large datasets
- Precision: Target exact locations using line numbers or patterns
- Automation: Perfect for scripts that modify multiple files
- Flexibility: Works with pipes, redirects, and other Unix tools
Master sed’s complete toolkit for text manipulation:
- Delete lines using the
d
command - Insert or append text with
i
anda
commands (this guide) - Transform text case for standardization
- Search and replace text with pattern matching
Inserting Text with sed
The i
command inserts text before a specified line. This is perfect for adding headers, comments, or configuration entries at precise locations.
Basic Insert Syntax
sed 'LINE_NUMBER i TEXT' filename
Insert by Line Number
Insert before specific line:
sed '4i This is the inserted line.' example.txt
This inserts text before line 4.
Insert at beginning of file:
sed '1i Header line goes here' filename
Insert multiple lines:
sed '4i First inserted line\
Second inserted line\
Third inserted line' filename
Insert by Pattern Matching
Insert before lines matching a pattern:
sed '/PATTERN/i TEXT' filename
Practical examples:
sed '/function main/i # Main function starts here' script.py
sed '/^server {/i # Nginx server configuration' nginx.conf
sed '/export PATH/i # Adding to PATH variable' .bashrc
Advanced Insert Examples
Insert with variables:
header="Generated on $(date)"
sed "1i $header" datafile.txt
Insert configuration blocks:
sed '/^# Database settings/i \
# Redis configuration\
redis.host=localhost\
redis.port=6379' config.ini
Insert after finding specific content:
sed '/TODO:/i # FIXME: Review this section' code.py
Key Points
- Text is inserted before the specified line or pattern
- Original line numbers shift down after insertion
- Use
\
at line end for multi-line insertions - sed displays output to stdout by default (use
-i
for in-place editing)
Appending Text with sed
The a
command appends text after a specified line. This is ideal for adding footers, closing tags, or additional data following specific content.
Basic Append Syntax
sed 'LINE_NUMBER a TEXT' filename
Append by Line Number
Append after specific line:
sed '2a Don'\''t forget to subscribe!' filename
This appends text after line 2.
Append at end of file:
sed '$a Footer text goes here' filename
Append multiple lines:
sed '4a\
First appended line\
Second appended line\
Third appended line' filename
Append by Pattern Matching
Append after lines matching a pattern:
sed '/PATTERN/a TEXT' filename
Practical examples:
sed '/^}$/a # End of function block' script.js
sed '/^server {/a # Server configuration continues' nginx.conf
sed '/export PATH/a # PATH modified above' .bashrc
Advanced Append Examples
Append configuration sections:
sed '/^# Database settings/a \
host=localhost\
port=5432\
database=myapp' config.ini
Append with variables:
timestamp="Last modified: $(date)"
sed "\$a $timestamp" datafile.txt
Append after specific markers:
sed '/<!-- INSERT HERE -->/a <div>New content</div>' template.html
Append vs Insert Comparison
Command | Position | Example |
---|---|---|
i | Before line/pattern | sed '5i text' → inserts before line 5 |
a | After line/pattern | sed '5a text' → appends after line 5 |
Key Points
- Text is appended after the specified line or pattern
- Use
$
to append at end of file - Use
\
at line end for multi-line appends - Original line numbers remain unchanged (new lines added below)
Targeting Specific Positions
sed’s power lies in its ability to precisely target where text should be inserted or appended using various addressing methods.
Position-Based Targeting
Line numbers:
sed '1i Header text' filename # Insert at beginning
sed '5a Middle text' filename # Append after line 5
sed '$a Footer text' filename # Append at end
Line ranges:
sed '10,15i # Section start' filename # Insert before lines 10-15
sed '20,$a # End section' filename # Append after line 20 to end
Pattern-Based Targeting
Simple patterns:
sed '/TODO/i # FIXME: Address this' filename
sed '/^function/a # Function definition ends' filename
sed '/^#/a # Comment continues' filename
Complex patterns with regular expressions:
sed '/^[0-9]/i # Numbered item:' filename # Before lines starting with digits
sed '/\.log$/a # Log entry processed' filename # After lines ending with .log
sed '/^[[:space:]]*$/i # Empty line above' filename # Before blank lines
Practical Use Cases
Configuration files:
# Add database configuration
sed '/^# Database/a \
host=localhost\
port=5432\
user=admin' config.ini
# Insert security headers
sed '/^server {/a \
add_header X-Frame-Options SAMEORIGIN;\
add_header X-Content-Type-Options nosniff;' nginx.conf
Code files:
# Add function documentation
sed '/^def /i # Function: Performs calculation' script.py
# Insert debugging statements
sed '/^if /a print("Debug: Condition checked")' debug.py
Data processing:
# Add CSV headers
sed '1i Name,Age,Email' data.csv
# Insert separators
sed '/^---/i # Section divider' document.txt
Advanced Targeting Techniques
Conditional insertion:
# Insert only if pattern exists
sed '/config_section/{ i # Configuration starts here
}' filename
Multiple operations:
# Insert and append around the same pattern
sed '/important_line/{ i # Important section begins
a # Important section ends
}' filename
Using variables for dynamic content:
section_name="Database Configuration"
sed "/^# $section_name/a host=localhost" config.ini
Safety and Testing
Preview changes:
sed '/pattern/i NEW TEXT' filename # Shows output without changing file
sed '/pattern/i NEW TEXT' filename | head # Preview first few lines
Test with line numbers:
nl filename | sed '/pattern/i NEW TEXT' # Show line numbers for context
Safe file modification:
sed -i.backup '/pattern/i NEW TEXT' filename # Creates filename.backup
Advanced Insert and Append Techniques
Once you master the basics, sed’s advanced features unlock powerful text manipulation capabilities for complex scenarios.
Conditional Text Addition
Insert only when specific conditions are met:
# Insert warning before error lines
sed '/ERROR/i *** WARNING: Critical error detected ***' logfile.txt
# Append configuration after specific sections
sed '/^# Network settings/a \
interface=eth0\
dhcp=true' config.txt
Multiple condition matching:
# Insert before lines starting with specific patterns
sed '/^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}/i Date entry:' dates.txt
# Append after lines containing both keywords
sed '/database.*config/a # Database configuration processed' app.conf
Multi-Line Text Blocks
Insert complex configuration blocks:
sed '/^# SSL Configuration/i \
# SSL Certificate Setup\
ssl_certificate /path/to/cert.pem;\
ssl_certificate_key /path/to/key.pem;\
ssl_protocols TLSv1.2 TLSv1.3;' nginx.conf
Append structured data:
sed '/^{$/a \
"name": "default",\
"version": "1.0",\
"active": true' config.json
Dynamic Content Insertion
Using variables and command substitution:
# Insert timestamp
current_time=$(date)
sed "1i Generated on: $current_time" report.txt
# Append system information
sed "\$a System: $(uname -s), User: $(whoami)" logfile.txt
Insert file contents:
# Insert entire file content
sed '/INCLUDE_POINT/r include.txt' main.txt
# Combine with text insertion
sed '/HEADER/{ i # Configuration file begins
r config_template.txt
a # Configuration file ends
}' main.conf
Pattern Range Operations
Insert/append within specific ranges:
# Insert text before each line in a range
sed '10,20i # Line in middle section' filename
# Append after pattern range
sed '/START/,/END/a # Block processed' filename
Advanced Scripting Techniques
Multiple operations in sequence:
sed -e '/function/i # Function definition' \
-e '/function/a # Function body starts' \
-e '/return/i # Function ends' \
-e '/return/a # Return statement processed' script.py
Using sed scripts for complex operations:
Create modify.sed
:
/^# Database/i # Database Configuration Section
/^# Database/a host=localhost
/^# Database/a port=5432
/^# Security/i # Security Settings Section
/^# Security/a enable_ssl=true
Run with:
sed -f modify.sed config.ini
Practical Advanced Examples
Log file processing:
# Add timestamps and separators
sed -e '/ERROR/i === ERROR DETECTED ===' \
-e '/ERROR/a Timestamp: '$(date)'' \
-e '/WARN/i --- Warning ---' application.log
HTML/XML processing:
# Insert DOCTYPE and meta tags
sed -e '1i <!DOCTYPE html>' \
-e '/<head>/a <meta charset="UTF-8">' \
-e '/<head>/a <meta name="viewport" content="width=device-width, initial-scale=1.0">' index.html
Code documentation automation:
# Add function documentation
sed '/^def /i \
# Function documentation\
# TODO: Add parameter descriptions\
# TODO: Add return value description' script.py
Performance and Safety
Test complex operations step by step:
# Test first operation
sed '/pattern/i TEXT1' file.txt | head -10
# Add second operation
sed -e '/pattern/i TEXT1' -e '/other/a TEXT2' file.txt | head -10
Use intermediate files for complex workflows:
sed '/pattern/i TEXT' original.txt > temp1.txt
sed '/other/a MORE_TEXT' temp1.txt > temp2.txt
sed '/final/i FINAL_TEXT' temp2.txt > result.txt
Backup strategy for advanced operations:
cp original.txt original.txt.$(date +%Y%m%d_%H%M%S)
sed -i -f complex_script.sed original.txt
Regular Expression Integration
Advanced pattern matching:
# Insert before lines matching complex patterns
sed '/^[A-Z][a-z]*[0-9]\{2,4\}/i # Code identifier found' data.txt
# Append after email patterns
sed '/[a-zA-Z0-9._%+-]\+@[a-zA-Z0-9.-]\+\.[a-zA-Z]\{2,\}/a # Email processed' contacts.txt
Pro tip: For extremely complex text manipulation, consider combining sed with other tools like awk, or writing dedicated scripts for better maintainability and debugging capabilities.
Conclusion
Mastering sed’s insert and append commands transforms you into an efficient text editing expert. You now have the skills to:
- Insert text precisely before specific lines or patterns using the
i
command - Append text accurately after target locations using the
a
command - Handle complex scenarios with advanced pattern matching and multi-line text
- Automate text modifications safely with proper testing and backups
Key Takeaways
- Use
i
to insert text before lines/patterns - Use
a
to append text after lines/patterns - Test commands first without the
-i
flag - Create backups when editing files in-place
- Leverage patterns for dynamic and flexible text placement
Best Practices Checklist
- ✅ Preview changes before applying permanently
- ✅ Use backups with
-i.bak
for safety - ✅ Test on samples before processing important files
- ✅ Escape special characters properly in patterns
- ✅ Document complex commands for future reference
When to Use Each Command
Scenario | Command | Example |
---|---|---|
Add header/title | i | sed '1i # File Header' file.txt |
Add footer/signature | a | sed '$a # End of file' file.txt |
Insert before errors | i | sed '/ERROR/i *** ALERT ***' log.txt |
Append after config | a | sed '/^server/a port=8080' config.txt |
Master the Complete sed Toolkit
Expand your sed expertise with related techniques:
- Delete lines - Remove unwanted content
- Transform text case - Standardize capitalization
- Search and replace - Pattern-based substitution
With these insert and append techniques mastered, you’re equipped to add content precisely, automate file modifications, and maintain text files efficiently across any Unix-like system.
Quick Reference Guide
Common Insert Commands
# Insert at specific positions
sed '1i Header text' file.txt # Before first line
sed '5i Middle text' file.txt # Before line 5
sed '/pattern/i Before text' file.txt # Before matching lines
# Multi-line insert
sed '1i\
Line 1\
Line 2\
Line 3' file.txt
Common Append Commands
# Append at specific positions
sed '5a After text' file.txt # After line 5
sed '$a Footer text' file.txt # After last line
sed '/pattern/a After text' file.txt # After matching lines
# Multi-line append
sed '$a\
Footer line 1\
Footer line 2\
Footer line 3' file.txt
Frequently Asked Questions
Q: What’s the difference between insert and append?
A: Insert (i
) adds text before the target line/pattern. Append (a
) adds text after the target line/pattern.
Q: How do I add multi-line text?
A: Use backslashes at the end of each line: sed '1i\ Line 1\ Line 2' file.txt
Q: Can I insert variables in the text?
A: Yes, use double quotes: sed "1i Current date: $(date)" file.txt
Q: How do I make changes permanent?
A: Use the -i
flag: sed -i '1i Header' file.txt
(consider -i.bak
for backups)
Q: What happens to line numbers after insertion? A: Insert: Line numbers shift down. Append: Line numbers stay the same, new lines added below.
Q: Can I insert/append to multiple files at once?
A: Yes: sed -i '1i Header' *.txt
or use find: find . -name "*.txt" -exec sed -i '1i Header' {} \;
Related Posts

Search and Replace Lines with Sed: Master Advanced Techniques
Master sed's search and replace functionality with practical examples - find, substitute, and transform text efficiently.

How to Exclude Directories or Files When Copying to a Remote Machine
Learn how to exclude specific files and directories when copying to remote servers using rsync and scp commands with practical examples.

How to Check Remote Ports are Reachable Using ‘nc’ Command
Learn how to check if remote ports are opened in linux with nc (netcat) command.