devutils/ATFQ/book ATFQ git

Part III · Shell and commands

28.4 sed

alsosedreplace textsearch and replacefind and replacesubstitute

TL;DR

  • sed edits text line by line as it flows through. Its most used command is s/old/new/.
  • Without g, only the first match on each line changes; s/old/new/g changes them all.
  • -i edits the file in place. GNU sed (Linux) takes -i alone; macOS needs -i ''.
sed 's/http:/https:/g' links.txt
sed -i.bak 's/DEBUG=1/DEBUG=0/' .env    # keeps a copy in .env.bak
sed -n '10,20p' big.log                 # print lines 10 to 20

Pitfalls

Deep dive 2 questions

How does sed work?

It reads one line, runs the script on it, prints the result, then moves to the next line. -n turns off the automatic printing, so only p prints.

A command can be limited to some lines with an address in front: /^#/d deletes comment lines, 5,10s/a/b/ only replaces on lines 5 to 10, and /start/,/end/ covers everything between two matches.

How do I reuse part of the match?

With -E, parentheses capture, and \1, \2 bring the captures back in the replacement. This swaps "user@host" into "host: user":

sed -E 's/([a-z]+)@([a-z.]+)/\2: \1/' contacts.txt
Commands
sed
See also
28.2 grep and regex
28.3 awk