sed (COMP2041)

Sed is a unix command-line based utility that does essentially find+replace.

It takes input in the format:

sed -flags 's/string1/string2/endflag'


  • sed 's/hello/hello world/' will replace the first instance of 'hello' with 'hello world'


There are a bunch of flags for sed, but the only really useful one is:

  • -n = surpress the natural printing of every line to stdout

End Flags

At the end of the expression we can add an end flag. These change the operation of sed:

  • /g = match all instances, not just the first
  • /p = print lines that were modified

Other Extensions

Using parenthesis i.e. "(" or ")" we can enclose a section of the string1, and use it as an argument in string 2.


sed -n 's/^\([a-z]*\)\.html$/\1/p'

This will take any number of lowercase alphabetic characters followed by a .html, and only print the lowercase characters.

E.g. "hello.html" would only print "hello".
The -n is stopping the output from printing the original, and the /p is printing the modified.


Things such as parenthesis () and dots . must be escaped to be used in sed. Escaping just means placing a \ before them.