August 26, 2017

UNIX - Use sed to edit files

sed is one of the commands that have been around forever on Unix systems. sed is a stream editor that you can use to perform basic text transformations on an input stream (a file or input from a pipeline).

Want to use sed to edit a file in place? Well, to replace every ’e’ with an ‘o’, in a file named ‘foo’, you can do:

sed -i.bak s/e/o/g foo

And you’ll get a backup of the original in a file named ‘foo.bak’, but if you want no backup:

sed -i '' s/e/o/g foo

Want to strip UTF-8 BOM(Byte Order Mark) from given files?

sed -e '1s/^\xef\xbb\xbf//' < bomfile > newfile

It is handy to know that you can use any character as a seperator instead of the default / because it can be hard to follow. e.g

echo "/home/example" | sed 's#/home/example#/usr/local/example#'

instead of

echo "/home/example" | sed 's/\/home\/example/\/usr\/local\/example/'

You can use regular expressions in sed. See man regex.

© Arnold Greyling 2023