SED substitute backreference example

Posted in Bash, SED on May 10, 2018 · 1 min read
This tutorial goes over how to use Sed's substitute command with some global expressions that are backreferenced to output a correct HTML image path.

Sometimes I forget how to properly backreference in sed when I use substitute.  To solve this dilemma I will post an example to reference in the future and I hope it will help others. If you haven't used substitute in sed it is completed with s/ / /.

Creating an example lets change foo into bar with:

echo foo | sed s/foo/bar/

Before getting into some examples we should talk about the delimiter and how to use it.  The delimiter is the slash that follows the s  in the substitution but what if you have multiple paths in a line you are trying to replace such as mac/scripts/foo/bar/etc/etc?  If you were to use this in the substitution you would get errors when trying to run the script.  To properly address this you would do mac\/scripts\/foo\/bar\/etc\/etc  but this becomes very hard to read.  There are some other alternatives you could use instead of the slash:

sed 's|mac/scripts/foo/bar/etc/etc|output|' foo.txt bar.txt
sed 's_mac/scripts/foo/bar/etc/etc_output_' foo.txt bar.txt
sed 's:mac/scripts/foo/bar/etc/etc:output:' foo.txt bar.txt

Depending on the amount I am scripting I prefer using the colon over the pipe since the pipe is commonly used in conjunction with other terminal commands or scripts.

Now time for some an examples.

Say we have an image tag that has a source but is missing the alt tag and we want to correct this in sed it would look like:

# original:
<img src="../image/foobar.png"/>

# sed:
sed -e 's:src\="\.\.\/image\/\([A-Za-z0-9_]*\)\.\([A-Za-z0-9]*\)":src\="\.\.\/image\/\1\.\2" alt\="\1" :g' foo.txt bar.txt

# result:
<img src="../image/foobar.png" alt="foobar" />

Now if the colons were not in place in the above code it would be very difficult to figure out where the substitution started and ended.

Help the site go ad free:
Storm Trooper