Docs / Linux Basics / Using sed for Text Processing

Using sed for Text Processing

By Admin · Feb 25, 2026 · Updated Apr 24, 2026 · 29 views · 1 min read

What is sed?

sed (stream editor) is a powerful command-line tool for parsing and transforming text. It processes text line by line, making it perfect for automated edits in scripts.

Basic Substitution

# Replace first occurrence per line
sed "s/old/new/" file.txt

# Replace all occurrences per line
sed "s/old/new/g" file.txt

# Case-insensitive
sed "s/old/new/gi" file.txt

# Edit file in place
sed -i "s/old/new/g" file.txt

Common Use Cases

# Change config values
sed -i "s/listen_port=80/listen_port=8080/" config.ini

# Delete lines matching pattern
sed -i "/^#/d" config.conf  # Remove comments
sed -i "/^$/d" file.txt     # Remove empty lines

# Insert line after match
sed -i "/\[server\]/a listen_port=8080" config.ini

# Print only matching lines
sed -n "/error/p" logfile.log

Address Ranges

# Apply to line range
sed "5,10s/foo/bar/g" file.txt

# Apply from pattern to end
sed "/START/,$ s/foo/bar/g" file.txt

Was this article helpful?