On this page 12 sections
  1. How sed works
  2. Substitute text
  3. Case-insensitive replacement
  4. Use alternative delimiters
  5. Modify a file in place
  6. Delete lines
  7. Print selected lines
  8. Work with ranges
  9. Capture groups
  10. Remove leading and trailing whitespace
  11. Append and insert lines
  12. Multiple sed expressions
01

How sed works

sed is a stream editor. It reads text line by line, applies editing instructions and writes the result to standard output. By default it does not modify the original file.

Basic substitution
sed 's/http/https/' urls.txt
02

Substitute text

The s command replaces text. Without additional flags only the first occurrence on each line is replaced.

Replace first occurrence
sed 's/dev/prod/' config.txt
Replace every occurrence
sed 's/dev/prod/g' config.txt
03

Case-insensitive replacement

GNU sed supports the I flag for case-insensitive matching.

Case-insensitive replacement
sed 's/error/warning/gI' application.log
04

Use alternative delimiters

The slash character is conventional but not mandatory. Different delimiters make replacements involving filesystem paths or URLs much easier to read.

Replace URL
sed 's#http://sam0x.me#https://sam0x.me#g' config.txt
05

Modify a file in place

-i writes changes back to the file. Creating a backup suffix before editing important configurations is safer.

Edit in place
sed -i 's/dev/prod/g' config.txt
Edit and create backup
sed -i.bak 's/dev/prod/g' config.txt
06

Delete lines

The d command removes matching lines from the output.

Delete blank lines
sed '/^$/d' file.txt
Delete comments
sed '/^[[:space:]]*#/d' config.txt
Remove comments and empty lines
sed '/^[[:space:]]*#/d; /^[[:space:]]*$/d' config.txt
07

Print selected lines

-n disables automatic output. The p command can then print only the lines or ranges you explicitly select.

Print line 10
sed -n '10p' file.txt
Print lines 10 to 20
sed -n '10,20p' file.txt
Print lines containing ERROR
sed -n '/ERROR/p' application.log
08

Work with ranges

sed can apply commands only to a numeric range or between two matching patterns.

Delete first five lines
sed '1,5d' file.txt
Print configuration block
sed -n '/BEGIN CONFIG/,/END CONFIG/p' file.txt
09

Capture groups

Extended regular expressions combined with capture groups make it possible to extract and rearrange parts of a line.

Extract username from key=value
echo 'user=sam0x' | sed -E 's/^user=(.*)$/\1/'
Swap two fields
echo 'sam0x:admin' | sed -E 's/^([^:]+):([^:]+)$/\2:\1/'
10

Remove leading and trailing whitespace

Whitespace cleanup is a common sed use case when normalising command or configuration output.

Trim leading whitespace
sed 's/^[[:space:]]*//' file.txt
Trim trailing whitespace
sed 's/[[:space:]]*$//' file.txt
11

Append and insert lines

a appends text after a selected line while i inserts text before it.

Append after match
sed '/server_name/a\    # managed by sam0x' nginx.conf
Insert before match
sed '/server_name/i\    # virtual host' nginx.conf
12

Multiple sed expressions

-e allows several transformations to be applied in sequence.

Clean comments and blank lines
sed -e '/^[[:space:]]*#/d' -e '/^[[:space:]]*$/d' config.txt