Mastering the sed Command in Linux: Edit, Replace, and Transform Text
The sed command in Linux is a simple text editor used to make small transformations to file content. It uses the POSIX ERE pattern for regular expressions.
Sed receives text from one or more files passed as arguments on the command line and transforms it, sending the modification to standard output STDOUT (video monitor).
If we want sed to actually change the file content, we need to use the greater-than redirector ">" to another file.
Another possibility is to use the option "-iSUFFIX" which allows editing the original file directly and saving a backup copy with the file name followed by the indicated SUFFIX.
The sed options are:
- -iSUXIFO changes the file
- -e prints to screen without changing the file
- -n suppresses output, shows only the command result
- -f file Reads a script with sed expressions
The commands that sed accepts are:
- s replaces a text string with another
- ! inverts the command logic
- ; command separator
- | string separator
- d at the end deletes
- p at the end prints
- g at the end (as used with d and p) changes all occurrences
To replace occurrences with other text, the sed "s" option can be used. The normal forward slashes "/" are used to separate the text to be searched and the new text. In this case, the replacement is sent to standard output STDOUT and the file remains intact:
$ sed "s/velho/novo/" arquivo
To replace the name uira with the name carla in the /etc/passwd file and print to standard output:
$ sed ‘s/uira/carla/’ /etc/passwd
In the example below, the occurrence /usr/local/bin is changed to /usr/bin and written to textonovo.txt without altering the original file. The use of the backslash was necessary to indicate that the character "'/" in the path is not the sed replacement separator:
$ sed ‘s/\/usr\/local\/bin/\/usr\/bin/’ texto.txt > textonovo.txt
You can choose to use the -i option, so that sed will change the file and keep a backup of the original file in arquivo.bkp
$ sed –i.bkp ‘s/\/usr\/local\/bin/\/usr\/bin/’ arquivo
Puts the word "comprar" at the beginning of each line:
$ sed 's/^/comprar /' supermercadocomprar arrozcomprar feijão
comprar carnecomprar batatacomprar alfacecomprar tomatecomprar arroz
Prints only the third line of the supermercado file:
$ sed -n '3p' supermercado carne
Prints from the third to the fifth line of the supermercado file:
$ sed -n '3,5p' supermercado carnebatataalface
The "d" command can be used to delete all lines containing the word carne and create a copy:
$ sed -i.bkp '/carne/d' supermercado $ cat supermercadoarrozfeijãobatataalfacetomatearroz
Replaces all occurrences of arroz and batata with couve-flor:
$ sed 's/arroz\|batata/couve-flor/' supermercadocouve-florfeijãocouve-floralfacetomatecouve-flor
Deletes all blank lines, altering the file:
$ sed -i '/^$/d' arquivo.txt
Removes all HTML TAGs from the file:
$ sed 's/<[^>]*>//g' arquivo.txt
A set of sed expressions can be saved to a text file to be read by it as an interpreter with the -f option. In this case, several sed commands were saved to a file called expressoes:
$ cat expressoess/carla/uira/s/<p>/<p align=center>/
$ sed -f expressoes arquivo.txt
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.