Command linehardBash

Advanced Sed Text Transformation

Advanced sed patterns for complex text manipulation, line editing, and batch processing.

01

The problem

You need to perform complex text transformations, conditional replacements, or multi-line editing.

02

The solution

Bash
# Basic substitution
sed 's/old/new/g' file.txt

# Replace only on specific lines
sed '5,10s/old/new/g' file.txt        # Lines 5-10
sed '/pattern/s/old/new/g' file.txt   # Lines containing pattern

# Multiple replacements
sed -e 's/old/new/g' -e 's/foo/bar/g' file.txt
sed 's/old/new/g; s/foo/bar/g' file.txt

# Use different delimiter
sed 's|/usr/bin|/usr/local/bin|g' file.txt
sed 's#/old/path#/new/path#g' file.txt

# Backreferences
sed 's/\(word\)/\U\1/' file.txt            # Uppercase first word
sed 's/\(.*\):\(.*\)/\2:\1/' file.txt    # Swap columns

# Case conversion
sed 's/.*/\L&/' file.txt      # Entire line to lowercase
sed 's/.*/\U&/' file.txt      # Entire line to uppercase
sed 's/\b\(.\)/\U\1/g' file.txt  # Title case words

# Delete operations
sed '/pattern/d' file.txt                      # Delete matching lines
sed '/start/,/end/d' file.txt                  # Delete range
sed '5,10d' file.txt                           # Delete lines 5-10

# Insert/append text
sed '3i\New line before line 3' file.txt      # Insert before
sed '3a\New line after line 3' file.txt       # Append after
sed '/pattern/a\New line' file.txt            # Append after pattern

# Change entire line
sed '/pattern/c\Replacement line' file.txt
sed '5c\New line 5' file.txt

# Print specific lines
sed -n '10,20p' file.txt                       # Print lines 10-20
sed -n '/start/,/end/p' file.txt               # Print range
sed -n '/pattern/p' file.txt                   # Print matching lines

# In-place editing (with backup)
sed -i.bak 's/old/new/g' file.txt
sed -i '' 's/old/new/g' file.txt               # macOS (no backup)

# Read from another file
sed '/pattern/r otherfile.txt' file.txt

# Write to another file
sed '/pattern/w output.txt' file.txt

# Hold space operations (advanced)
sed '1!G;h;$!d' file.txt                       # Reverse file
sed -n 'G; s/\n/&&/; /^\n\n/d; s/\n//; h; P' file.txt  # Uniq adjacent

# Number lines
sed = file.txt | sed 'N;s/\n/\t/'            # Add line numbers

# Join lines
sed ':a;N;$!ba;s/\n/ /g' file.txt             # All lines to one
sed 'N;s/\n/ /' file.txt                      # Join pairs

# Conditional branching
sed '/pattern/{n;s/old/new/;}' file.txt        # Next line after pattern
sed '/start/,/end/{s/old/new/g;}' file.txt     # Only in range

# Multi-line patterns
sed '/first/{N;/first\nsecond/s/old/new/;}' file.txt

# Address with regex
sed '/^#/d' file.txt                           # Delete comment lines
sed '/^$/d' file.txt                           # Delete empty lines
sed '/^[[:space:]]*$/d' file.txt               # Delete blank lines

# Transform specific columns
sed 's/\([^,]*\),\{0,1\}/\U\1,/g' file.csv  # Uppercase first column

03

Put it to work

Example
# Convert CSV to TSV
sed 's/,/\t/g' data.csv > data.tsv

# Remove HTML tags
sed 's/<[^>]*>//g' file.html

# Format log files
sed -n '/ERROR/p' /var/log/syslog | sed 's/.*ERROR: //'

# Update configuration
sed -i '/^ServerName/c\ServerName www.example.com' httpd.conf

# Extract specific section from config
sed -n '/^<VirtualHost/,/^<\/VirtualHost/p' apache.conf

# Swap first and last columns
sed 's/^\([^ ]*\) \(.*\) \([^ ]*\)$/\3 \2 \1/' data.txt

# Convert markdown headers
sed 's/^\(#\{1,6\} \)/\U&/' README.md

# Clean up CSV (remove quotes, extra spaces)
sed 's/"//g; s/ *, */,/g; s/^[[:space:]]*//' data.csv

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

# Smart indentation fix
sed 's/^    /\t/' code.py                    # 4 spaces to tab

Worth knowing

Use -i.bak for safety. Test with -n p first. & represents matched pattern. \1-\9 are backreferences. : label; b branch; N next line; h/H hold; g/G get. For complex tasks, consider awk or perl.