Master Jenkins UserRemoteConfig for dynamic Git repository management. Includes Groovy examples, …
Sed Command Cheat Sheet: 30 Essential One-Liners for Text Processing
Summary
Sed Command Cheat Sheet: 30 Essential One-Liners for Text Processing
Terminal showing sed command">
Whether you’re a developer, system administrator, or DevOps engineer, text processing is an essential part of your daily work. The stream editor sed
remains one of the most powerful tools for manipulating text from the command line, but its syntax can be cryptic and difficult to remember.
As Robert Love notes in “Linux System Programming”, mastering command-line text processing dramatically increases productivity in system programming tasks. After years of working with text manipulation, I’ve compiled this comprehensive cheat sheet of 30 essential sed
one-liners that I use regularly.
This reference guide organizes commands by function and complexity, making it easy to find the right tool for your text processing challenges. Each command includes a clear explanation and practical example to help you understand not just the syntax, but how and when to apply it.
Let’s dive into these powerful text manipulation commands!
Basic Text Substitution
These fundamental substitution commands form the backbone of sed’s text processing capabilities.
1. Basic Substitution (First Occurrence per Line)
sed 's/old/new/' file.txt
Example: Replace the first occurrence of “error” with “warning” on each line
sed 's/error/warning/' logs.txt
2. Global Substitution (All Occurrences)
sed 's/old/new/g' file.txt
Example: Replace all occurrences of “color” with “colour” throughout the file
sed 's/color/colour/g' document.txt
3. Case-Insensitive Substitution
sed 's/old/new/gi' file.txt
Example: Replace all occurrences of “user” (in any case) with “customer”
sed 's/user/customer/gi' users.txt
4. Substituting with Confirmation
sed -i.bak 's/old/new/g' file.txt
Example: Replace all “http://” with “https://” and create a backup
sed -i.bak 's/http:\/\//https:\/\//g' urls.txt
5. Using Alternative Delimiters
sed 's|old|new|g' file.txt
Example: Replace file paths with cleaner syntax
Expand your knowledge with How to Replace Text in Multiple Files with Sed: A Step-by-Step Guide
sed 's|/var/www/old/|/var/www/new/|g' config.txt
Line Selection and Filtering
These commands help you target specific lines for operations based on line numbers, patterns, or ranges.
6. Apply Command to Specific Line by Number
sed '5s/old/new/' file.txt
Example: Replace “username” with “user_id” only on line 5
sed '5s/username/user_id/' config.txt
7. Apply Command to a Range of Lines
sed '5,10s/old/new/g' file.txt
Example: Add prefix to lines 5-10
sed '5,10s/^/PREFIX: /' log.txt
8. Apply Command to Lines Matching a Pattern
sed '/pattern/s/old/new/g' file.txt
Example: Replace “disabled” with “enabled” only in lines containing “feature”
sed '/feature/s/disabled/enabled/g' settings.txt
9. Apply Command from Pattern to End of File
sed '/pattern/,$s/old/new/g' file.txt
Example: Update version in all configuration blocks after the main section
sed '/\[main\]/,$s/version=1\.0/version=2.0/g' config.ini
10. Apply Command to Range Between Two Patterns
sed '/start_pattern/,/end_pattern/s/old/new/g' file.txt
Example: Update database settings only within the database configuration block
Deepen your understanding in Alternatives to envsubst: Finding the Right Templating Solution for Your CI/CD Pipelines
sed '/\[database\]/,/\[/s/host=localhost/host=db.example.com/g' config.ini
Line Deletion and Insertion
These commands add or remove entire lines, essential for cleaning up files or adding new content.
11. Delete Lines Matching a Pattern
sed '/pattern/d' file.txt
Example: Remove all comment lines
sed '/^#/d' config.txt
12. Delete a Specific Line by Number
sed '5d' file.txt
Example: Remove the header line (line 1)
sed '1d' data.csv
13. Delete a Range of Lines
sed '5,10d' file.txt
Example: Remove lines 1-5 (like skipping a header)
sed '1,5d' report.txt
14. Delete Empty Lines
sed '/^$/d' file.txt
Example: Clean up a file by removing all blank lines
sed '/^$/d' script.sh
15. Delete Lines Matching Multiple Patterns
sed '/pattern1/d; /pattern2/d' file.txt
Example: Remove both commented lines and empty lines
sed '/^#/d; /^$/d' config.txt
16. Append Text After a Match
sed '/pattern/a\new text' file.txt
Example: Add a warning after any configuration line containing “deprecated”
sed '/deprecated/a\# WARNING: This setting will be removed in the next version' config.txt
17. Insert Text Before a Match
sed '/pattern/i\new text' file.txt
Example: Add a header before the main configuration section
sed '/\[main\]/i\# Main Configuration - Updated 2023-08-01' config.ini
18. Replace an Entire Line
sed '/pattern/c\new line' file.txt
Example: Replace version line with new version
Explore this further in Security Considerations When Using envsubst: Protecting Your CI/CD Pipeline
sed '/^version=/c\version=2.0.0' app.properties
Advanced Text Manipulation
These powerful commands handle more complex text manipulation requirements.
19. Using Capture Groups in Substitutions
sed 's/\(pattern\).*$/\1 new text/' file.txt
Example: Keep usernames but standardize the email domain
sed 's/\(user[^@]*\)@.*/\1@company.com/' emails.txt
20. Multiple Substitutions in One Command
sed -e 's/old1/new1/g' -e 's/old2/new2/g' file.txt
Example: Replace multiple HTML tags in one pass
sed -e 's/<h1>/# /g' -e 's/<h2>/## /g' -e 's/<\/h[12]>//g' html.txt
21. Removing Leading Whitespace
sed 's/^[ \t]*//' file.txt
Example: Fix indentation in a text file
sed 's/^[ \t]*//' poorly_formatted.txt
22. Removing Trailing Whitespace
sed 's/[ \t]*$//' file.txt
Example: Clean up trailing spaces from code
sed 's/[ \t]*$//' code.js
23. Adding Line Numbers
sed = file.txt | sed 'N;s/\n/\t/'
Example: Create a numbered list of items
sed = todo.txt | sed 'N;s/\n/. /'
24. Converting DOS/Windows Line Endings to Unix
sed 's/\r$//' file.txt
Example: Fix line endings in a script transferred from Windows
sed 's/\r$//' windows_script.sh
25. Swapping the Order of Two Words
sed 's/\(word1\) \(word2\)/\2 \1/g' file.txt
Example: Reverse “first name, last name” to “last name, first name”
Discover related concepts in How to Replace Text in Multiple Files with Sed: A Step-by-Step Guide
sed 's/\([^,]*\), \(.*\)/\2 \1/' names.txt
Advanced sed Techniques
These techniques showcase some of sed’s more powerful capabilities for complex text processing.
26. Printing Specific Lines Only
sed -n '/pattern/p' file.txt
Example: Extract all error messages from a log file
sed -n '/ERROR:/p' application.log
27. Applying Multiple Commands to the Same Pattern
sed '/pattern/{s/old/new/g; s/foo/bar/g}' file.txt
Example: Update multiple fields in a JSON configuration
sed '/"database":/,/}/{s/"host": "localhost"/"host": "db.example.com"/; s/"port": 3306/"port": 5432/}' config.json
28. Conditional Command Execution
sed '/pattern/{/another/d;}' file.txt
Example: Remove debug messages from production logs only
sed '/\[PRODUCTION\]/{/DEBUG:/d;}' application.log
29. Working with CSV Using Hold Space
sed 'h; s/[^,]*,//; G; s/\n/,/; s/,\([^,]*\),.*/,\1/' file.csv
Example: Swap first and last columns in a two-column CSV
sed 'h; s/\([^,]*\),\(.*\)/\2/; x; s/,.*/,/; G; s/\n//' ids_names.csv
30. Processing Multi-line Blocks
sed -n '/start/,/end/p' file.txt
Example: Extract complete XML tags with their content
Uncover more details in Advanced Bash Scripting Techniques for Automation: A Comprehensive Guide
sed -n '/<user>/,/<\/user>/p' users.xml
Real-World Examples: Solving Common Tasks
Now let’s see how these commands can be applied to real-world tasks that developers and system administrators face daily.
Example 1: Updating Configuration Files
# Change server settings in Apache config
sed -i.bak \
-e 's/^Listen 80$/Listen 8080/' \
-e '/^#.*SSL/s/^#//' \
-e '/DocumentRoot/s|/var/www/html|/var/www/app|' \
httpd.conf
This complex example:
- Changes the listen port from 80 to 8080
- Uncomments all SSL-related settings
- Updates the document root path
- Creates a backup of the original file
Example 2: Log File Analysis
# Extract error timestamps and messages
sed -n '/ERROR/s/\(.*\) ERROR: \(.*\)/\1 | \2/p' application.log
This command:
- Finds lines containing “ERROR”
- Reformats them to show just the timestamp and message
- Uses capture groups to preserve and rearrange the important information
Example 3: Data Cleaning for CSV Files
# Clean and format a CSV file for import
sed -e 's/^[ \t]*//' -e 's/[ \t]*$//' -e 's/"//g' -e '/^$/d' -e 's/,\{2,\}/,NA,/g' data.csv
This command chain:
- Removes leading whitespace
- Removes trailing whitespace
- Removes all quotation marks
- Deletes empty lines
- Replaces consecutive commas with “,NA,” to properly handle missing values
Example 4: HTML Conversion to Markdown
# Convert simple HTML to Markdown
sed -e 's/<h1>\(.*\)<\/h1>/# \1/g' \
-e 's/<h2>\(.*\)<\/h2>/## \1/g' \
-e 's/<b>\(.*\)<\/b>/**\1**/g' \
-e 's/<i>\(.*\)<\/i>/*\1*/g' \
-e 's/<a href="\([^"]*\)">\(.*\)<\/a>/[\2](\1)/g' \
simple.html
This complex transformation:
Journey deeper into this topic with Troubleshooting common EC2 issues
- Converts HTML headers to Markdown headers
- Converts bold and italic tags to Markdown syntax
- Transforms HTML links to Markdown link format
Understanding Sed’s Syntax Deeply
To truly master sed
, it’s important to understand its core syntax components. Brian Ward’s “How Linux Works” breaks down the sed command structure as follows:
sed [options] 'address1[,address2][!]{command}' file
Where:
- options modify sed’s behavior (like
-i
for in-place editing) - address specifies which lines to operate on (line numbers or patterns)
- ! inverts the line selection (operate on all lines EXCEPT those selected)
- command is the operation to perform (like ’s’ for substitute)
For example:
sed -i '5,10!s/old/new/g' file.txt
This applies the substitution to all lines EXCEPT lines 5 through 10.
Enrich your learning with A Comprehensive Guide to Understanding DevOps
Sed Options You Should Know
These options dramatically extend sed’s capabilities:
-i[suffix] - Edit files in place (with optional backup)
sed -i.bak 's/old/new/g' file.txt
-e - Specify multiple commands
sed -e 's/1/one/g' -e 's/2/two/g' file.txt
-f - Read commands from a file
sed -f commands.sed file.txt
-n - Suppress automatic printing
sed -n '/pattern/p' file.txt
-r or -E - Use extended regular expressions
Gain comprehensive insights from So You Want to Be a Data Analyst? Here's What You Need to Know
sed -E 's/([0-9]+)/Number: \1/g' file.txt
Best Practices from the Experts
According to “Unix Power Tools” and “Shell Scripting” guides, these practices will make your sed commands more robust:
- Test commands without
-i
first to preview changes before modifying files. - Use alternative delimiters like
|
or@
when your pattern contains slashes. - Create backups with
-i.bak
for important files. - Comment complex sed scripts for better maintainability.
- Prefer multiple simple commands over a single complex one for readability.
Robert Love’s “Linux System Programming” emphasizes that while these one-liners are powerful, complex text processing might be better handled by purpose-built tools for specific formats.
Master this concept through 15 Jenkins DevOps Best Practices Every Team Should Follow
When to Use Sed vs. Alternative Tools
This quick reference helps decide when to use sed versus other text processing tools:
Sed is best for line-based text transformations and search & replace operations.
# Example use case
sed 's/error/warning/g' log.txt
Awk excels at column-based data processing and complex logic.
# Example use case
awk '$3 > 100 {print $1}' data.txt
Grep is ideal for pattern searching and line filtering.
# Example use case
grep -A 3 "ERROR" log.txt
Perl handles complex regex and multi-line patterns extremely well.
# Example use case
perl -0pe 's/start(.*)end/new/s' file.txt
Jq is specialized for JSON processing.
Delve into specifics at Harnessing Chaos: An Overview of Tools and Libraries for Chaos Engineering
# Example use case
jq '.users[] | .name' data.json
Portable Sed Commands for Cross-Platform Use
When working across different Unix systems, be aware of GNU vs. BSD sed differences. These commands work on both:
Deepen your understanding in Alternatives to envsubst: Finding the Right Templating Solution for Your CI/CD Pipelines
# Portable in-place edit (works on Linux and macOS)
sed -i.bak 's/old/new/g' file.txt && rm file.txt.bak
# Portable line selection (without GNU extensions)
sed -n '10,20p' file.txt
# Portable extended regex (without -E or -r)
sed 's/[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\}/XXX-XX-XXXX/g' file.txt
Creating a Sed Command Library
Many experienced Unix admins, as noted in “Unix Power Tools”, keep a file of useful sed commands. Here’s how to start yours:
Deepen your understanding in Alternatives to envsubst: Finding the Right Templating Solution for Your CI/CD Pipelines
# Create a sed commands file
mkdir -p ~/bin/sed-scripts
# Example: Save a useful command
cat > ~/bin/sed-scripts/xml-to-json.sed << 'EOF'
# Convert simple XML to JSON
s/<\([a-zA-Z]*\)>/{\"\1\":/g
s/<\/[a-zA-Z]*>/},/g
s/>\([^{]*\)</>\"\1\"</g
EOF
# Usage
sed -f ~/bin/sed-scripts/xml-to-json.sed file.xml
Conclusion: Mastering Sed for Everyday Text Processing
This cheat sheet has covered 30 essential sed commands that every developer, system administrator, or DevOps engineer should have in their toolkit. From basic substitutions to advanced text manipulations, these one-liners will dramatically increase your productivity when processing text files.
As Brian Ward mentions in “How Linux Works”, mastering command-line text processing is a fundamental skill that separates novice from expert Unix users. By adding these commands to your repertoire, you’ll be able to handle a wide range of text processing tasks efficiently.
For convenience, here’s a downloadable version of this cheat sheet that you can keep handy at your workstation:
Download Sed Command Cheat Sheet (TXT)
What are your favorite sed one-liners? Share them in the comments below to help expand this reference for the community!
Similar Articles
Related Content
More from devops
Explore how OpenAI transforms development workflows, empowering developers and DevOps teams with …
Discover the best Linux automation tools like Ansible, Terraform, and Docker. Learn how to automate …
You Might Also Like
Master the art of replacing text across multiple files with sed. This step-by-step guide covers …
Discover how to implement Google's Site Reliability Engineering (SRE) principles using Bash scripts. …
Discover how to extract filenames from paths in Bash using commands like basename, dirname, and …
Knowledge Quiz
Test your general knowledge with this quick quiz!
The quiz consists of 5 multiple-choice questions.
Take as much time as you need.
Your score will be shown at the end.
Question 1 of 5
Quiz Complete!
Your score: 0 out of 5
Loading next question...
Contents
- Basic Text Substitution
- Line Selection and Filtering
- Line Deletion and Insertion
- Advanced Text Manipulation
- Advanced sed Techniques
- Real-World Examples: Solving Common Tasks
- Understanding Sed’s Syntax Deeply
- Sed Options You Should Know
- Best Practices from the Experts
- When to Use Sed vs. Alternative Tools
- Portable Sed Commands for Cross-Platform Use
- Creating a Sed Command Library
- Conclusion: Mastering Sed for Everyday Text Processing