The sed command in Linux is a simple text editor used to make small transformations in the content of the files. It uses the POSIX ERE standard for regular expressions.
sed takes text from one or more files passed as an argument on the command line and transforms it by sending the modification to the standard STDOUT (video monitor) output.
If we want sed to actually change the content of the file, it is necessary to use the redirector larger than “>” for any other file.
Another possibility is to use the “-Suffix” option, which allows you to directly edit the original file and save a backup copy with the file name followed by the indicated SUFFIX.
The sed options are:
The commands that sed accepts are:
To exchange occurrences with another text, the sed option “s” can be used. The normal “/” slashes are used to separate the text to be searched for and the new text. In this case, the swap is sent to the standard STDOUT output and the file remains intact:
$ sed “s/old/new/” file
To change the name Uira to the name Carla in the /etc/passwd file and print in the standard output:
$ sed 's/uira/carla/' /etc/passwd
In this example below the instance /usr/local/bin is changed to /usr/bin and written to textonovo.txt without altering the original file. The use of the slash counter was necessary to indicate that the path character “’/” is not the replacement separator for sed:
$ sed 's/ /usr /local /bin/ /usr /bin/' texto.txt > textonovo.txt
You can choose to use the —i option, so sed will alter the file and maintain a backup of the original file in the.bkp file
$ sed —i.bkp 's/ /usr /local /bin/ /usr /bin/' file
Put the word “buy” at the beginning of each line:
$ sed 's/^/buy /' supermarket
buy rice
buy beans Buy meat
buy potatoes
buy lettuce
buy tomatoes
buy rice
Print only the third line of the supermarket file:
$ sed -n '3p' meat supermarket
Print from the third to the fifth line of the supermarket file:
$ sed -n '3,5p' supermarket meat potato lettuce
The “d” command can be used to delete all lines containing the word meat and create a copy:
$ sed -i.bkp '/meat/d' supermarket $ cat supermarket rice beans potato lettuce tomato rice
Swap all occurrences of rice and potatoes for cauliflower:
$ sed 's/rice |potatoes/cauliflower/' supermarket cauliflower beans cauliflower lettuce tomato cauliflower
Erase all blank lines, changing the file:
$ sed -i '/^$/d' arquivo.txt
Remove all HTML TAGs from the file:
$ sed 's/] *>//g' arquivo.txt
A set of sed expressions can be written to a text file to be read by it as an interpreter with the -f option. In this case, several sed commands were written to a file called expressions:
$ cat expressions
s/carla/uira/s/
/
$ sed -f expressions arquivo.txt
Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.
Share
The sudo command on Linux executes a given command as if it were another user.…
The ss command on Linux is extremely useful for investigating sockets, providing various information about…
Free Linux command shows the amount of total memory in use and available, as well…
The shell has structures for testing conditions and executing certain program sequences several times (loop),…
The /etc/fstab file stores the configuration of which devices should be mounted and what is…
The Netcat Command in Linux or nc is a utility used to do “almost anything”…
This website uses cookies.