SED is a powerful stream editor for filtering and transforming text. You can perform impressive and efficient operations with such tool.

In this post, we will discover the wonders of SED.

Text conversion and substitution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# match "pattern" against the pattern space and replace the 1st occurrence of each line
# with "replacement" if successful matched
sed 's/pattern/replacement/'
# you can refer the pattern matching sub-expressions with special escapes \1 through \9
# /!\ parenthesis and plus sign must be escaped, otherwise sed will consider it as a simple character
sed 's/^foo\(\w\+\)/\1/'

# replace only the 4th instance of each line by adding a number
sed 's/pattern/replacement/4'

# replace ALL instances of each file by adding "g"
sed 's/pattern/replacement/g'

# find lines that contain "foobar" and replace "pattern" with "replacement"
sed '/foobar/s/pattern/replacement/g'

# search "pattern" and substitute with "replacement" except lines containing "foobar"
sed '/foobar/!s/pattern/replacement/g'

# if you want to edit files in place, add the flag "-i"
sed -i 's/pattern/replacement/g' filename.txt

Selective deletion

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# remove last character
sed '$s/.$//'

# delete last line
sed '$d'

# delete lines matching pattern
sed '/pattern/d'

# delete 2nd line
sed '2d'

# delete the first 10 lines
sed '1,10d'

# delete ALL blank lines
sed '/^$/d'

# replace commas with newlines (use \r, not \n)
sed 's/,/\r/g'

# insert 2 blank spaces at beginning of each line
sed 's/^/  /'

Print

1
2
3
4
5
6
7
# -n flag hides lines not affected by the expression, often useful in print statements

# print lines that contain "pattern"
sed -n '/pattern/p'

# print 2nd line
sed -n '2p'

Tricks

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# you can perform multiple sed operations
# instead of
sed 's/foo/bar/g' | sed 's/bar/popo/g'

# you can do this
sed -e 's/foo/bar/g' -e 's/bar/popo/g'

# or even this
sed 's/foo/bar/g;s/bar/popo/g'

# you can substitute "/" with another character
sed 's~foo~bar~'

# append "foobar" after lines matching "pattern"
sed '/pattern/a foobar'
# prepend "foobar" before lines matching "pattern"
sed '/pattern/i foobar'

# append "foobar" after 2nd line
sed '2a foobar'
# prepend "foobar" before 2nd line
sed '2i foobar'