LUNAROPS · OPERATIONAL UPLINK 100% UPTIME 1,247d POSTS 893 JEFF.MOON@LUNAROPS.DEV UTC --:--:--

sed: Stream Editor Essentials

sedtext-processingregexcli

sed (stream editor) transforms text line by line. It’s perfect for find-and-replace operations and text manipulation.

Basic Substitution

1
2
3
4
5
6
7
8
# Replace first occurrence per line
sed 's/old/new/' file.txt

# Replace all occurrences (global)
sed 's/old/new/g' file.txt

# Replace nth occurrence
sed 's/old/new/2' file.txt  # Second occurrence

In-Place Editing

1
2
3
4
5
# Edit file directly
sed -i 's/old/new/g' file.txt

# With backup (safer)
sed -i.bak 's/old/new/g' file.txt

macOS note: Requires sed -i '' 's/old/new/g' file.txt

Delimiters

Any character can be a delimiter:

1
2
3
# Useful for paths
sed 's|/usr/local|/opt|g' file.txt
sed 's#old#new#g' file.txt

Case Insensitive

1
sed 's/error/ERROR/gi' file.txt

Regular Expressions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Match any character
sed 's/h.t/cold/g' file.txt  # hat, hot, hit → cold

# Match pattern
sed 's/[0-9]/#/g' file.txt  # Replace digits

# Capture groups
sed 's/\(.*\):\(.*\)/\2:\1/' file.txt  # Swap before:after

# Extended regex
sed -E 's/([0-9]+)/[\1]/g' file.txt  # Wrap numbers in brackets

Address Ranges

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Specific line
sed '5s/old/new/' file.txt  # Line 5 only

# Line range
sed '5,10s/old/new/' file.txt  # Lines 5-10

# From line to end
sed '5,$s/old/new/' file.txt

# Lines matching pattern
sed '/error/s/old/new/' file.txt

# Between patterns
sed '/START/,/END/s/old/new/' file.txt

Delete Lines

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Delete specific line
sed '5d' file.txt

# Delete line range
sed '5,10d' file.txt

# Delete matching lines
sed '/pattern/d' file.txt

# Delete empty lines
sed '/^$/d' file.txt

# Delete lines NOT matching
sed '/pattern/!d' file.txt

Insert and Append

1
2
3
4
5
6
7
8
# Insert before line 3
sed '3i\New line here' file.txt

# Append after line 3
sed '3a\New line here' file.txt

# Insert before matching line
sed '/pattern/i\New line' file.txt
1
2
3
4
5
6
7
8
# Print specific lines
sed -n '5p' file.txt

# Print range
sed -n '5,10p' file.txt

# Print matching lines
sed -n '/pattern/p' file.txt

Multiple Commands

1
2
3
4
5
6
7
8
# Using semicolon
sed 's/a/A/g; s/b/B/g' file.txt

# Using -e
sed -e 's/a/A/g' -e 's/b/B/g' file.txt

# From file
sed -f commands.sed file.txt

Practical Examples

Remove Comments

1
2
3
4
5
# Remove # comments
sed '/^#/d' file.txt

# Remove and blank lines
sed '/^#/d; /^$/d' file.txt

Remove Whitespace

1
2
3
4
5
6
7
8
# Leading whitespace
sed 's/^[ \t]*//' file.txt

# Trailing whitespace
sed 's/[ \t]*$//' file.txt

# Both
sed 's/^[ \t]*//; s/[ \t]*$//' file.txt

Add Line Numbers

1
sed = file.txt | sed 'N;s/\n/\t/'

Convert Line Endings

1
2
3
4
5
# Windows to Unix (remove \r)
sed 's/\r$//' file.txt

# Unix to Windows (add \r)
sed 's/$/'$'\r'/' file.txt

Extract Between Patterns

1
2
3
4
5
# Print between START and END
sed -n '/START/,/END/p' file.txt

# Excluding the markers
sed -n '/START/,/END/{/START/!{/END/!p}}' file.txt

Wrap Lines

1
2
# Add prefix and suffix
sed 's/.*/<li>&<\/li>/' file.txt

Number Formatting

1
2
3
# Add commas to numbers
echo "1234567" | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta'
# Output: 1,234,567

Replace in Specific Section

1
2
# Only in [section]
sed '/^\[section\]/,/^\[/s/old/new/' config.ini

Multiple Files

1
2
3
4
5
# Process multiple files
sed -i 's/old/new/g' *.txt

# With find
find . -name "*.txt" -exec sed -i 's/old/new/g' {} \;

Debugging

1
2
3
4
5
# Show what would change
sed 's/old/new/g' file.txt | diff file.txt -

# Print before and after
sed -n 's/old/new/p' file.txt

Cheat Sheet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
sed 's/old/new/'           # Replace first
sed 's/old/new/g'          # Replace all
sed -i 's/old/new/g'       # In-place edit
sed '5d'                   # Delete line 5
sed '/pattern/d'           # Delete matching
sed -n '/pattern/p'        # Print matching
sed '1,5s/old/new/'        # Lines 1-5
sed '/^$/d'                # Delete empty lines
sed 's/^/prefix/'          # Add prefix
sed 's/$/suffix/'          # Add suffix

sed is terse but powerful. Combined with grep and awk, it handles most text processing needs.

Comments