Sed Tutorial

Thu 02 October 2014
By alex

Sed's primary function is to search for and replace text; we call this process "substitution". Additionally, sed can easily append, insert and delete text. The syntax can be somewhat intimidating to the new user.

sed 'command/pattern/replacement/flags' file.txt
sed '\pattern\|line_number command/input_text/' file.txt

The command field tells sed what to do when a match is found; the pattern field is the search string; the replacement field should be obvious and the flag defines various options such as how many times to act on a search hit. The tricky part lies in the fact that sed has multiple syntaxes. When performing operations on entire lines the command field is prefixed with the target line number(s) and the pattern field is not used.

Typical Use

sed -i 's/herf/href/g' /var/www/index.html 

The example replaces all instances of herf with href, pretty slick huh? Let's take a look at some of the other things Sed can do.

Substitution

We just saw this in the last example. Here we replace all instances of foo with bar

sed -i 's/foo/bar/g' file.txt

Deletion

Sed can also delete specific lines of text. The following removes line 10 from file.txt

sed -i '10d' file.txt

Range Deletion

A range of lines can be deleted at once using D. The example deletes line 95-10.

sed -i '95,100D' file.txt

Append

Strings can be appended after a specific line. This example appends Bazinga to a new line after line 20

sed -i '20a\Bazinga\' file.txt

Insertion

Sed can insert text before specifed lines as well. Here we insert Incoming into line 20 thus moving line 20's contents to line 21

sed -i '20i\Incoming\' file.txt

Replacement

Sed can also performs replacement of entire lines. Using the c command replaces the entire line with the specified string.

sed -i '10c\Presto\' file.txt

Pattern Match Deletion

Another deletion feature of Sed's is deleting lines that matches a pattern. The example deletes every line that contains the string false

sed -i '/false/d'  file.txt


Advanced Sed

Sed's additional syntax (insert, append, delete, and replace):

sed /pattern/|line number i\ a\ d\ c\input_text\ filename

Find a string and delete the line it's in

sed -i '/error/d/' /var/log/tunnel.log
  • /d deletes a line that matches the pattern, it's part of sed's bizarre syntax

Find a string and insert stuff into the line before it

sed -i '/^exit/ i\iptables-restore < /etc/firewall.rules\' /etc/rc.local
  • The additional syntax accepts line numbers and ranges, but matching regexes is much cleaner

Find a string and replace the entire line

sed -i "s/^dc_relay_net.*$/dc_relay_net='$IP'/" /etc/exim4/update-exim4.conf.conf
  • -i actions are performed inline, directly within the file specified
  • " " are used so $IP will expand AND it's contents will be surrounded by ' ' when finished
  • The entire line gets replaced because of creative use of regex matching
  • ^ matches the beginning of a line, .* matches everything, $ matches the end of a line, duh
  • sed -i "/dc_relay_net/ c\dc_relay_net='$IP'/" would work except the " "s mess up the command and they're needed due to using $IP