sed Notes

Stream editing, substitution, addressing, regex, and safe file cleanup

Table of Contents


Introduction

What is sed?

sed stands for stream editor. It is a non-interactive text processing tool used to transform input from a file or pipeline.

sed reads input line by line, applies operations like find and replace, delete, or insert, and prints the result without opening an interactive editor.

Why sed?

Reason Explanation
Speed Processes massive files line-by-line without loading entire file into memory
Automation Perfect for scripts - no manual editing needed
Precision Regex-powered pattern matching for surgical edits
Pipelines Works seamlessly with other tools (grep, awk, cat, etc.)
Portability Available on virtually every Unix/Linux system

History - Who Created sed?

sed was one of the earliest tools to support regular expressions (regex), making it a pioneer in text processing.

Who Uses sed?

Role Use Case
SysAdmins Config file modifications, log parsing
DevOps Engineers CI/CD pipeline text transformations
Penetration Testers Parsing tool outputs, cleaning wordlists
Developers Batch code refactoring, data cleaning
Data Analysts CSV/text preprocessing

Installation

Check if sed is Installed

sed --version
# or
which sed

Installation by OS

Linux (Debian/Ubuntu/Kali)

# Usually pre-installed.  If not: 
sudo apt update && sudo apt install sed

Linux (Fedora/RHEL/CentOS)

sudo dnf install sed
# or
sudo yum install sed

macOS

# macOS has BSD sed (slightly different syntax)
# For GNU sed (recommended for compatibility):
brew install gnu-sed

# Use as 'gsed' or add to PATH: 
export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"

Windows

Option 1: WSL (Recommended)

# Enable WSL, install Ubuntu, sed comes pre-installed
wsl --install

Option 2: Git Bash - Install Git for Windows (includes sed)

Option 3: GnuWin32 - Download from gnuwin32.sourceforge.net

Option 4: Cygwin - Install Cygwin with sed package

Local setup note: On Kali Linux, sed is installed by default. Open a terminal and verify it before starting the exercises.

General Syntax

sed [OPTIONS] 'COMMAND' [INPUT_FILE]

Or with pipeline:

cat file.txt | sed 'COMMAND'

Syntax Breakdown

Component Description
sed The command itself
[OPTIONS] Flags that modify behavior (-i, -n, -e, etc.)
'COMMAND' The operation to perform (in single quotes)
[INPUT_FILE] File to process (optional if using pipeline)

Command Structure

sed '[ADDRESS]OPERATION[/PATTERN/REPLACEMENT/[FLAGS]]'
Part Meaning
ADDRESS Which lines to operate on (line number, range, or pattern)
OPERATION What to do (s=substitute, d=delete, p=print, etc. )
PATTERN What to find (supports regex)
REPLACEMENT What to replace with
FLAGS Modifiers (g=global, i=case-insensitive, etc.)

All Options & Flags

Command-Line Options

Flag Long Form Description
-n --quiet / --silent Suppress automatic printing; only print when explicitly requested with p
-e --expression=SCRIPT Add multiple commands in one sed call
-f --file=SCRIPT_FILE Read commands from a file
-i --in-place[=SUFFIX] Edit file in-place (modify original file)
-i.bak - Edit in-place but create backup with .bak extension
-r / -E --regexp-extended Use Extended Regular Expressions (ERE)
-s --separate Treat each file as separate stream
-u --unbuffered Load minimal data and flush output buffers frequently
-z --null-data Separate lines by NUL character (not newline)
--help - Display help message
--version - Display version info
-i modifies the original file! Always test without -i first or use -i.bak for backup.

Substitution Flags (used with s command)

Flag Description
g Global - Replace all occurrences in line (not just first)
p Print - Print the modified line
i / I Case-insensitive matching
NUMBER Replace only the Nth occurrence (e.g., 2 = second match)
w FILE Write - Write modified lines to FILE

Core Concepts

1. Pattern Space vs Hold Space

Space Description
Pattern Space Temporary buffer where current line is held for processing
Hold Space Secondary buffer for storing data across lines (advanced)
Think of Pattern Space as your "workspace" and Hold Space as a "clipboard."

2. Addressing - Targeting Specific Lines

Address Type Example Description
Line Number 5 Only line 5
Line Range 5,10 Lines 5 through 10
Start to End 5,$ Line 5 to last line ($ = last line)
Pattern /error/ Lines containing "error"
Pattern Range /start/,/end/ From line matching "start" to line matching "end"
Step 1~2 Every odd line (start at 1, step by 2)
Negation 5! All lines EXCEPT line 5

3. sed Commands (Operations)

Command Name Description
s Substitute Find and replace
d Delete Remove lines
p Print Output lines (use with -n)
a Append Add text AFTER current line
i Insert Add text BEFORE current line
c Change Replace entire line with new text
y Transform Character-by-character replacement (like tr)
q Quit Stop processing after current line
r Read Insert contents of another file
w Write Write pattern space to file
= Line Number Print current line number
n Next Skip to next line
N Append Next Append next line to pattern space
h Hold Copy pattern space to hold space
H Hold Append Append pattern space to hold space
g Get Copy hold space to pattern space
G Get Append Append hold space to pattern space
x Exchange Swap pattern and hold spaces

4. Regular Expressions in sed

Basic Regex (BRE) - Default

Symbol Meaning
. Any single character
* Zero or more of previous character
^ Start of line
$ End of line
[ ] Character class
[^ ] Negated character class
\( \) Capture group (escaped parentheses)
\1, \2 Backreference to capture group
\{n\} Exactly n occurrences
\{n,m\} Between n and m occurrences

Extended Regex (ERE) - Use with -E or -r

Symbol Meaning
+ One or more of previous character
? Zero or one of previous character
( ) Capture group (no escaping needed)
| OR operator
{n,m} Quantifier (no escaping needed)

5. Delimiters

The / is the default delimiter, but you can use ANY character:

# Default
sed 's/old/new/'

# Using | as delimiter (useful when pattern contains /)
sed 's|/path/to/old|/path/to/new|'

# Using # as delimiter
sed 's#old#new#'

# Using @ as delimiter
sed 's@old@new@'
When working with file paths, use | or # to avoid escaping slashes.

6. Special Characters in Replacement

Character Meaning
& The entire matched pattern
\1 - \9 Backreference to capture groups
\U Uppercase the replacement
\L Lowercase the replacement
\u Uppercase first character
\l Lowercase first character
\E End case conversion

Sample Data Setup

Create a file called emp.csv with 35 employee records and 7 fields:

Fields: EmpID, Name, Department, Designation, Salary, JoinDate, City

cat << 'EOF' > emp.csv
EmpID,Name,Department,Designation,Salary,JoinDate,City
E001,Raj Kumar,IT,Developer,75000,2020-03-15,Mumbai
E002,Priya Sharma,HR,Manager,85000,2019-07-22,Delhi
E003,Amit Singh,IT,Senior Developer,95000,2018-01-10,Bangalore
E004,Sneha Patel,Finance,Analyst,65000,2021-05-18,Chennai
E005,Vikram Reddy,IT,Team Lead,110000,2017-09-05,Hyderabad
E006,Anjali Gupta,Marketing,Executive,55000,2022-02-14,Pune
E007,Rahul Verma,IT,Developer,72000,2020-08-30,Mumbai
E008,Kavita Nair,HR,Recruiter,48000,2021-11-25,Kochi
E009,Suresh Menon,Finance,Manager,98000,2016-04-12,Bangalore
E010,Deepa Krishnan,IT,Developer,70000,2020-06-08,Chennai
E011,Arjun Mehta,Sales,Executive,52000,2022-01-20,Delhi
E012,Pooja Iyer,IT,QA Engineer,68000,2019-10-15,Hyderabad
E013,Manoj Tiwari,Operations,Supervisor,62000,2018-12-03,Lucknow
E014,Ritu Saxena,Marketing,Manager,92000,2017-03-28,Mumbai
E015,Karthik Rajan,IT,DevOps Engineer,105000,2019-05-10,Bangalore
E016,Neha Agarwal,Finance,Analyst,63000,2021-07-14,Noida
E017,Sanjay Dubey,IT,Senior Developer,98000,2018-08-22,Pune
E018,Meera Joshi,HR,Manager,88000,2017-11-30,Mumbai
E019,Varun Kapoor,Sales,Manager,95000,2016-06-18,Delhi
E020,Lakshmi Rao,IT,Developer,74000,2020-04-25,Hyderabad
E021,Gaurav Mishra,Operations,Manager,90000,2018-02-08,Kolkata
E022,Swati Bose,Marketing,Analyst,58000,2021-09-12,Kolkata
E023,Nikhil Jain,IT,Architect,130000,2015-10-20,Bangalore
E024,Divya Pillai,Finance,Senior Analyst,82000,2019-01-15,Chennai
E025,Rohit Choudhary,IT,Team Lead,108000,2018-04-30,Gurgaon
E026,Aarti Deshmukh,HR,Executive,45000,2022-03-08,Pune
E027,Prasad Kulkarni,IT,Developer,71000,2020-12-18,Bangalore
E028,Nandini Hegde,Sales,Executive,54000,2021-06-22,Mysore
E029,Abhishek Pandey,IT,QA Lead,88000,2019-02-28,Noida
E030,Shruti Malhotra,Marketing,Senior Executive,72000,2020-01-10,Delhi
E031,Rajesh Nambiar,Finance,Manager,96000,2017-07-25,Kochi
E032,Pallavi Sinha,IT,Developer,69000,2021-03-05,Patna
E033,Venkat Subramanian,Operations,Analyst,60000,2020-09-14,Chennai
E034,Geeta Reddy,IT,Senior Developer,97000,2018-06-12,Hyderabad
E035,Anand Sharma,IT,CTO,180000,2014-01-01,Mumbai
EOF

Verify the file:

cat emp.csv | head -5      # View first 5 lines
wc -l emp.csv              # Should show 36 (header + 35 records)

Practice Exercises

Practice note: Try each exercise first, then check the solution below. This builds practical command memory.

SECTION A: Basic Substitution (s command)

Exercise A1: Simple Replacement
Replace all occurrences of "IT" with "Technology" in the output.
Show solution
sed 's/IT/Technology/g' emp.csv
  • s - substitute command
  • /IT/ - pattern to find
  • /Technology/ - replacement text
  • g - global flag (replace all occurrences in each line)
Exercise A2: Replace Only First Occurrence
Replace only the first occurrence of "Developer" with "Dev" in each line.
Show solution
sed 's/Developer/Dev/' emp.csv
  • Without g flag, only the first match per line is replaced.
Exercise A3: Replace Specific Occurrence
Replace only the 2nd occurrence of "a" with "@" in each line.
Show solution
sed 's/a/@/2' emp.csv
  • 2 - replace only the 2nd occurrence
Exercise A4: Case-Insensitive Replacement
Replace "manager" (any case) with "BOSS".
Show solution
sed 's/manager/BOSS/gi' emp.csv
  • i - case-insensitive matching
  • g - all occurrences
Exercise A5: Using Different Delimiters
Replace "2020-03-15" with "15/03/2020" (date format change).
Show solution
sed 's|2020-03-15|15/03/2020|' emp.csv
  • Using | as delimiter to avoid escaping / in the replacement

SECTION B: Addressing (Line Selection)

Exercise B1: Print Specific Line
Print only line 5 (E004's record).
Show solution
sed -n '5p' emp.csv
  • -n - suppress automatic printing
  • 5 - address (line 5)
  • p - print command
Exercise B2: Print Line Range
Print lines 10 to 15.
Show solution
sed -n '10,15p' emp.csv
  • 10,15 - range from line 10 to 15
Exercise B3: Print From Line to End
Print from line 30 to the last line.
Show solution
sed -n '30,$p' emp.csv
  • $ - represents the last line
Exercise B4: Print Every Odd Line
Print only odd-numbered lines (1, 3, 5, 7...).
Show solution
sed -n '1~2p' emp.csv
  • 1~2 - start at line 1, then every 2nd line
Exercise B5: Print Lines Matching Pattern
Print only lines containing "Mumbai".
Show solution
sed -n '/Mumbai/p' emp.csv
  • /Mumbai/ - pattern address
Exercise B6: Print Lines NOT Matching Pattern
Print all lines that do NOT contain "IT".
Show solution
sed -n '/IT/!p' emp.csv
  • ! - negation (NOT)

SECTION C: Delete Command (d)

Exercise C1: Delete Header Row
Delete the first line (header) and display the rest.
Show solution
sed '1d' emp.csv
  • 1 - line 1
  • d - delete command
Exercise C2: Delete Last Line
Delete the last line.
Show solution
sed '$d' emp.csv
Exercise C3: Delete Line Range
Delete lines 5 to 10.
Show solution
sed '5,10d' emp.csv
Exercise C4: Delete Lines Matching Pattern
Delete all employees from "Bangalore".
Show solution
sed '/Bangalore/d' emp.csv
Exercise C5: Delete Empty Lines
Delete all empty lines from a file.
Show solution
sed '/^$/d' emp.csv
  • ^$ - regex for empty line (start immediately followed by end)
Exercise C6: Delete Lines Matching Multiple Patterns
Delete lines containing "HR" OR "Finance".
Show solution
sed '/HR/d; /Finance/d' emp.csv
# OR
sed -e '/HR/d' -e '/Finance/d' emp.csv

SECTION D: Insert, Append, Change (i, a, c)

Exercise D1: Insert Line Before
Insert a comment line before the header.
Show solution
sed '1i # Employee Database - Confidential' emp.csv
  • 1 - line 1
  • i - insert before
  • Text follows immediately
Exercise D2: Append Line After
Append "--- END OF RECORDS ---" after the last line.
Show solution
sed '$a --- END OF RECORDS ---' emp.csv
Exercise D3: Change Entire Line
Replace line 5 with "RECORD REDACTED".
Show solution
sed '5c RECORD REDACTED' emp.csv
Exercise D4: Change Lines Matching Pattern
Replace any line containing "CTO" with "[EXECUTIVE DATA HIDDEN]".
Show solution
sed '/CTO/c [EXECUTIVE DATA HIDDEN]' emp.csv

SECTION E: Advanced Substitution (Regex & Backreferences)

Exercise E1: Extract Employee Names Only
Print only the Name field (2nd column).
Show solution
sed -n 's/^[^,]*,\([^,]*\),. */\1/p' emp.csv
  • ^[^,]*, - match EmpID and first comma
  • \([^,]*\) - capture group 1: Name (everything until next comma)
  • ,.* - match rest of line
  • \1 - backreference to captured Name
  • -n with p - only print modified lines

Alternative (Extended Regex):

sed -E -n 's/^[^,]*,([^,]*),.*/\1/p' emp.csv
Exercise E2: Swap First and Last Names
For names like "Raj Kumar", swap to "Kumar Raj".
Show solution
sed -E 's/,([A-Za-z]+) ([A-Za-z]+),/,\2 \1,/g' emp.csv
  • ([A-Za-z]+) - capture first name
  • - space
  • ([A-Za-z]+) - capture last name
  • \2 \1 - swap order
Exercise E3: Mask Salary (Privacy)
Replace salary amounts with "XXXXX" for privacy.
Show solution
sed -E 's/,[0-9]{5,6},/,XXXXX,/g' emp.csv
  • [0-9]{5,6} - match 5-6 digit numbers (salaries)
Exercise E4: Add Currency Symbol to Salary
Add "Rs." before each salary amount.
Show solution
sed -E 's/,([0-9]{5,6}),/,Rs.\1,/g' emp.csv
  • Capture the number, prefix with Rs. in replacement
Exercise E5: Using & (Matched Pattern)
Wrap all department names in square brackets.
Show solution
sed -E 's/,(IT|HR|Finance|Marketing|Sales|Operations),/,[\1],/g' emp.csv
  • & represents the entire matched pattern
  • Here we use capture group for precision
Exercise E6: Generate Email from Name
Create email addresses from names (e.g., "Raj Kumar" -> "[email protected]").
Show solution
# Extract names and create email format
sed -E -n 's/^[^,]*,([A-Za-z]+) ([A-Za-z]+),.*/\L\1.\2\[email protected]/p' emp.csv
  • \L...\E - convert to lowercase
  • \1.\2 - firstname.lastname format

SECTION F: Working with Multiple Commands

Exercise F1: Multiple Substitutions
Replace "IT" with "Tech" AND "HR" with "Human Resources".
Show solution
# Method 1: Semicolon separation
sed 's/IT/Tech/g; s/HR/Human Resources/g' emp.csv

# Method 2: Multiple -e flags
sed -e 's/IT/Tech/g' -e 's/HR/Human Resources/g' emp.csv

# Method 3: Using sed script file
echo -e 's/IT/Tech/g\ns/HR/Human Resources/g' > commands.sed
sed -f commands.sed emp.csv
Exercise F2: Delete and Replace in One Command
Delete the header AND replace "Developer" with "SDE".
Show solution
sed '1d; s/Developer/SDE/g' emp.csv

SECTION G: In-Place Editing (-i)

Exercise G1: Edit File with Backup
Replace all "Mumbai" with "Bombay" in the actual file (with backup).
Show solution
# Create backup first! 
cp emp.csv emp_backup.csv

# Edit in-place with backup
sed -i.bak 's/Mumbai/Bombay/g' emp.csv

# Verify
grep "Bombay" emp.csv
  • -i.bak - edit in-place, create backup with .bak extension
Warning: Using -i without backup suffix modifies the file permanently with no undo. Always test without -i first.
Exercise G2: macOS vs Linux In-Place Edit
Note the syntax difference between systems.
Show solution
# Linux (GNU sed)
sed -i 's/old/new/g' file.txt

# macOS (BSD sed) - requires empty string for no backup
sed -i '' 's/old/new/g' file.txt

# macOS with backup
sed -i '.bak' 's/old/new/g' file.txt

SECTION H: Real-World Scenarios

Exercise H1: CSV to Tab-Separated
Convert emp.csv from comma-separated to tab-separated.
Show solution
sed 's/,/\t/g' emp.csv > emp. tsv
Exercise H2: Remove Trailing Whitespace
Remove trailing spaces/tabs from each line.
Show solution
sed 's/[[:space:]]*$//' filename.txt
  • [[:space:]] - matches any whitespace (space, tab)
  • *$ - zero or more at end of line
Exercise H3: Remove Leading Whitespace
Remove leading spaces/tabs from each line.
Show solution
sed 's/^[[:space:]]*//' filename. txt
Exercise H4: Add Line Numbers
Add line numbers to each line.
Show solution
sed = emp.csv | sed 'N; s/\n/\t/'
  • = - prints line number
  • N - append next line to pattern space
  • s/\n/\t/ - replace newline with tab

Alternative (simpler tools):

nl emp.csv
# or
cat -n emp.csv
Exercise H5: Extract IT Department Only
Print only IT department employees.
Show solution
sed -n '/,IT,/p' emp.csv
Exercise H6: Print Lines Between Patterns
Print all lines between E010 and E020 (inclusive).
Show solution
sed -n '/^E010/,/^E020/p' emp.csv
  • /pattern1/,/pattern2/ - range between two patterns
Exercise H7: Comment Out Lines
Add "#" at the beginning of lines containing "Finance".
Show solution
sed '/Finance/s/^/#/' emp.csv
  • /Finance/ - address: lines containing Finance
  • s/^/#/ - substitute start of line with #
Exercise H8: Double Space a File
Add an empty line after each line.
Show solution
sed 'G' emp.csv
  • G - append hold space (empty by default) to pattern space
  • This adds a newline after each line
Exercise H9: Remove Double Spacing
Remove empty lines (single space the file).
Show solution
sed '/^$/d' emp.csv
Exercise H10: Uppercase All Text
Convert all text to uppercase.
Show solution
sed 's/. */\U&/' emp.csv
  • .* - match entire line
  • \U - uppercase what follows
  • & - the entire matched pattern

sed vs awk vs grep

Comparison Table

Task grep sed awk
Search/filter lines Yes Best Yes Good Yes Good
Simple find/replace No Yes Best Yes Good
Complex find/replace No Yes Best Yes Good
Field/column extraction No Warning Possible Yes Best
Numeric comparisons No No Yes Best
Arithmetic operations No No Yes Best
Line insertion/deletion No Yes Best Yes Good
In-place file editing No Yes Best Warning Limited
Speed on large files Yes Fastest Yes Fast Warning Slower

When to Use What?

Scenario Best Tool
"Find all lines with error" grep
"Replace all foo with bar" sed
"Print 3rd column of CSV" awk
"Sum of all values in column" awk
"Delete lines 5-10" sed
"Find IPs in log file" grep -E
"Transform date format" sed
"Filter rows where col3 > 100" awk

Combined Power Examples

# Example 1: Find and count IT employees
grep ",IT," emp.csv | wc -l

# Example 2: Extract IT employee names
grep ",IT," emp.csv | sed -E 's/^[^,]*,([^,]*),.*$/\1/'

# Example 3: Average salary of IT department
grep ",IT," emp.csv | awk -F',' '{sum+=$5; count++} END {print sum/count}'

# Example 4: Pipeline magic - IT devs in Mumbai, sorted by salary
grep ",IT," emp.csv | grep "Mumbai" | awk -F',' '{print $5, $2}' | sort -nr
Remember: These tools work BEST together in pipelines. Master each, then combine them for ultimate text processing power!

Challenge Exercises

Try these WITHOUT looking at solutions first! These test your complete understanding.

Challenge 1: Log File Cleanup

Create a fake log file and clean it:

echo -e "2024-01-01 INFO Starting\n2024-01-01 DEBUG Details\n2024-01-01 ERROR Failed\n2024-01-01 DEBUG More\n2024-01-01 INFO Done" > test. log

Task: Remove all DEBUG lines AND replace INFO with [INFO].

Show solution
sed '/DEBUG/d; s/INFO/[INFO]/g' test. log
Challenge 2: Config File Update

Create a config file:

echo -e "host=localhost\nport=8080\ndebug=true\ntimeout=30" > config.ini

Task: Change port to 3000 AND debug to false (in-place with backup).

Show solution
sed -i.bak 's/port=8080/port=3000/; s/debug=true/debug=false/' config.ini
Challenge 3: Data Masking

Task: In emp.csv, mask all dates to show only year (2020-03-15 -> 2020-XX-XX).

Show solution
sed -E 's/([0-9]{4})-[0-9]{2}-[0-9]{2}/\1-XX-XX/g' emp.csv
Challenge 4: Multi-line Header

Task: Add a 3-line header comment to emp.csv:

# Employee Database
# Last Updated: 2024
# Confidential
Show solution
sed '1i # Employee Database\n# Last Updated: 2024\n# Confidential' emp.csv

# Or with multiple -e: 
sed -e '1i # Employee Database' -e '1i # Last Updated:  2024' -e '1i # Confidential' emp.csv
Challenge 5: Extract and Transform

Task: Extract only CTO record, swap the name, and uppercase the designation.

Show solution
sed -n '/CTO/p' emp.csv | sed -E 's/,([A-Za-z]+) ([A-Za-z]+),/,\2 \1,/' | sed 's/CTO/\U&/'
Challenge 6: Numbering Non-Empty Lines

Task: Add line numbers only to lines that contain "Developer".

Show solution
sed -n '/Developer/=' emp.csv | sed 'N; s/\n/ - /'

# Better approach with grep + nl:
grep -n "Developer" emp.csv
Challenge 7: Reverse Pattern Match

Task: Print all lines that do NOT contain "Developer" AND do NOT contain "Manager".

Show solution
sed -n '/Developer/! {/Manager/! p}' emp.csv

# Or with delete: 
sed '/Developer/d; /Manager/d' emp.csv

Quick Reference Cheat Sheet

sed CHEAT SHEET

SUBSTITUTION

sed 's/old/new/'          # Replace first occurrence
sed 's/old/new/g'         # Replace all occurrences
sed 's/old/new/gi'        # Case-insensitive, all occurrences
sed 's/old/new/2'         # Replace 2nd occurrence only
sed 's/old/new/gp'        # Replace and print
sed 's|/old|/new|'        # Use | delimiter for paths

ADDRESSING

sed '5s/old/new/'         # Only on line 5
sed '5,10s/old/new/'      # Lines 5-10
sed '5,$s/old/new/'       # Line 5 to end
sed '/pattern/s/old/new/' # Lines matching pattern
sed '1~2s/old/new/'       # Every odd line
sed '0~2s/old/new/'       # Every even line

DELETION

sed '5d'                  # Delete line 5
sed '5,10d'               # Delete lines 5-10
sed '$d'                  # Delete last line
sed '/pattern/d'          # Delete matching lines
sed '/^$/d'               # Delete empty lines
sed '/^#/d'               # Delete comment lines

PRINTING

sed -n '5p'               # Print line 5 only
sed -n '5,10p'            # Print lines 5-10
sed -n '/pattern/p'       # Print matching lines
sed -n '/start/,/end/p'   # Print between patterns

INSERT / APPEND / CHANGE

sed '5i New line'         # Insert before line 5
sed '5a New line'         # Append after line 5
sed '5c New line'         # Replace line 5
sed '$a End of file'      # Append at end

IN-PLACE EDITING

sed -i 's/old/new/g' file      # Edit directly (Linux)
sed -i.bak 's/old/new/g' file  # Edit with backup
sed -i '' 's/old/new/g' file   # Edit directly (macOS)

MULTIPLE COMMANDS

sed 's/a/A/g; s/b/B/g'         # Semicolon separated
sed -e 's/a/A/' -e 's/b/B/'    # Multiple -e flags
sed -f script.sed file         # Commands from file

SPECIAL CHARACTERS

sed 's/. */[&]/'           # & = matched pattern
sed 's/\(word\)/[\1]/'    # \1 = backreference
sed 's/. */\U&/'           # Uppercase all
sed 's/. */\L&/'           # Lowercase all
sed 's/./\u&/'            # Capitalize first char

COMMON PATTERNS

sed 's/[[:space:]]*$//'   # Remove trailing whitespace
sed 's/^[[:space:]]*//'   # Remove leading whitespace
sed 'G'                   # Double space file
sed '/^$/d'               # Remove blank lines
sed 's/,/\t/g'            # CSV to TSV
sed -n '1p'               # Print first line (head -1)
sed -n '$p'               # Print last line (tail -1)

Key Takeaways

Keep these points in mind

  1. sed = stream editor - processes text line by line without loading the entire file
  2. Non-destructive by default - use -i for in-place editing carefully
  3. Regex-powered - regular expressions make sed much more useful
  4. Perfect for: find/replace, line manipulation, text transformation
  5. Not ideal for: numeric operations, complex logic (use awk instead)
  6. Test first - run without -i before modifying files
  7. Pipeline friendly - combines with grep, awk, sort, and other tools
  8. Delimiters are flexible - use | or # when dealing with paths
  9. Backups matter - use -i.bak when editing in place
  10. Practice with real text - logs, config files, and CSV files make the patterns easier to remember

Further Learning