Introduction
sed Is a utility that allows you to parse and transform text files. It was developed for Bell labs in the 70's and is now maintained by the GNU organisation.
sed Is a utility that allows you to parse and transform text files. It was developed for Bell labs in the 70's and is now maintained by the GNU organisation.
Most text editors have some form of user interface allowing you to browse through a file and make modifications. To allow you to read through the file most editors will load the entire file into the system memory. This may cause problems when these files are large in size.
sed Does not have a user interface like most editors. Instead it has its own small and simple programming language that allows you what actions to perform on a text file. Instead of attempting to load the entire file in memory, it uses streams to read and process every line separately. Allowing us to perform operations on files that would normally be much too big to edit with a conventional text editor.
More than often we need to search for a certain string in a text file and replace it with a different string. sed Can help us do that in a very efficient way.
Let's say we have a text file that contains the string "horse" and we need to replace all occurrences of that string with the string "donkey":
$ sed -i s/horse/donkey/g textfile.txt
We need to add the -i
option to specify that we want to edit the file "in place". This basically means that the modifications will be made to the existing file.
s/
Means we would like to search for the string that follows right after it.
Right after the search string we find a /
followed by the string we would like to be used instead of the search string.
After the replacement string there are the characters /g
basically the /
is the terminator signalling the end of the replacement string. The g
stands for global. Which means that we want to replace all occurrences of the search string, not just the first.