Sed is also a unix text manipulation tool most commonly used to replace the occurrence of one set of characters with another in a specified file. Sed also supports more complex regular expressions for efficent string manipulation.

Sed command syntax

sed s/java/ruby/g myfile.txt
Sed is commonly used without any command-line options, thus none are presented in this graphic for clarity

If the contents of myfile.txt were as follows:

Java is the best programming language,
thousands of programmers use Java everyday.

Running the above Sed command would produce the following output:

Ruby is the best programming language,
thousands of programmers use Ruby everyday.

Common Sed options

-i: Rewrite file(s) in-place instead of printing to standard output.
-E: Extended RegEx. More info

These are probably the most common options you’ll see used with Sed, but there are others.

Sed examples

String replacement

Replace all occurances of one string with another. In this case, replace all occurances of “Jack” with “Jill”

sed 's/Jack/Jill/g' rhyme.txt

Only replace the first occurance of a match in a line

Removing the trailing g from the Sed search pattern will tell Sed to only operate on the first occurance of the search pattern in a line

sed 's/Jack/Jill/' rhyme.txt

String replacement with RegEx

By default, the Sed search expression supports RegEx special characters; however, adding the -E flag prevents you from needing to escape characters like parenthesis.

sed -E 's/(.*) (.*)/\2, \1/' names.txt

In the above example, given I have a file of name formatted as “FIRST LAST”,

Kumail Nanjiani
Thomas Middleditch

I use parenthesis as capture groups, then rewrite the lines in the opposite order they appear as “LAST, FIRST”:

Nanjiani, Kumail 
Middleditch, Thomas

Regular expressions is a large topic. To dive deeper I recommend this resource.

Replace the contents of a file

Use the -i option to instruct Sed to overwrite the contents of rhyme.txt instead of printing to standard output.

sed -i 's/Jack/Jill/g' rhyme.txt

A note For MacOS terminal users who receive this error:
sed: -I or -i may not be used with stdin

The MacOS version of Sed requires that the -i be proceeded by an argument that specifies a backup file be created instead of overwriting the original. See the next example for instructions on how to do this…

Replace the contents of a file, but also create a backup

-i itself can take an optional argument that tells it to create a backup of the original file before making changes. In this case the file “rhyme.txt.original” will be created

sed -i.original 's/Jack/Jill/g' rhyme.txt

-i.original is short for --in-place=.original