The model: read, apply, print
sed keeps a pattern space, which holds the current line. For each input line it reads the line into the pattern space, runs every command whose address matches, and then prints the pattern space unless a command suppressed output. Nothing is changed in the file unless you ask for that explicitly. By default sed writes to standard output, so the original file is safe.
Because sed works on a stream, it can sit in the middle of a pipeline. It accepts the same input that grep and cut do, and it produces the same kind of output.
The substitute command
The command s/old/new/ replaces the first match on each line. A trailing g replaces every match on the line. The pattern is a BRE by default, and -E switches to ERE. Inside the replacement, & stands for the whole match, and \1, \2 refer to groups in the pattern.
echo 'at cat dog'| sed '/cat/bird/'
echo 'at cat dog'| sed '/cat/bird/g'The first command prints bird cat dog, because only the first match was replaced. The second prints bird bird dog. Forgetting the g is one of the most common reasons a substitution seems to do nothing on lines that contain more than one match.
Any character can be the delimiter in an s command. When the text contains slashes, as in a path, write s|/usr/bin|/bin| instead of escaping every slash.
Groups and references
With -E the parentheses form a group, and the replacement refers back to it. This is how you reorder fields. The example swaps a first and last name separated by a space.
echo 'da Lovelace'| sed -E '/([A-Za-z]+) ([A-Za-z]+)/\2, \1/'The output is Lovelace, Ada. The first group is Ada and the second is Lovelace, and the replacement prints them in the reverse order with a comma. In BRE the same pattern would need escaped parentheses.
Addresses: choosing lines
An address in front of a command selects the lines it applies to. A number selects one line, $ selects the last line, and a regular expression between slashes selects the lines that match. Two addresses separated by a comma select a range. The command p prints the pattern space, and -n turns off the automatic printing, so the combination -n with p prints only the selected lines.
printf 'lpha\nbeta\ngamma\ndelta\n'> list.txt
sed -n ',3p'list.txt
sed -n 'gam/p'list.txtThe first command prints beta and gamma, the lines two through three. The second prints gamma, the only line that matches the regex.
Deleting and inserting lines
The command d deletes the pattern space and starts the next cycle, so the line is not printed. Combined with an address, it removes lines. A common use is stripping comments and blank lines from a configuration file before reading it.
printf ' note\nkey=1\n\nkey=2\n'| sed '^#/d; /^$/d'The output is key=1 and key=2. The first address deletes lines that start with a hash, and the second deletes empty lines. Multiple commands are separated by a semicolon.
Several commands and the -e option
sed runs every command you give it on each line, in the order given. Commands can be joined with semicolons inside one script, or given one at a time with -e. Both forms produce the same result, and the second is easier to read when the commands are long.
printf ' name = old \n'| sed -e '/^ *//'-e '/ *$//'-e '/ = /=/'The output is name=old. The first command removes leading spaces, the second removes trailing spaces, and the third tightens the equals sign. The commands see the result of the previous one, so their order matters. Trimming first and then editing the text is the normal way to write such a chain.
A command that matches nothing is not an error. sed quietly passes the line through unchanged, so a typo in a pattern produces output that looks almost right. Compare the input and output with diff, or use sed -n with an explicit p to see exactly which lines were selected, before you rely on a script.
Think of each sed command as a rule with a condition. The address is the condition, and the command is the action. Without an address, the command applies to every line. This makes a scripted edit predictable: you can read a sed script the same way you read the rules of an awk program. When a rule is too broad, narrow its address before you change the command. For example, a substitution that should only touch lines inside a section can use a range such as /^\[server\]/,/^\[/ to restrict the match to that block.
In-place editing, carefully
The option -i writes the result back into the file instead of to standard output. It is convenient and also dangerous, because the original content is replaced. GNU sed accepts -i.bak to keep a backup named with the given suffix. Test the command without -i first, read the output, and only then edit the file. The same advice applies to every tool in this unit.
sed '/key/setting/'list.txt > list.new
diff list.txt list.newThis writes the change to a new file and then compares. The comparison shows nothing changed because list.txt has no key on any line. The habit of writing to a new file and diffing is safer than -i while you are learning.