Skip to content

sed command on Linux (alters texts)

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:

  • -iSuxiFo changes the file
  • -e prints on the screen without changing the -n file
  • ; does the suppression, shows only the result of the command
  • -f file Read a script with expressions from sed

The commands that sed accepts are:

  • s replace one piece of text with another
  • ! invert the logic of the command
  • ; command separator
  • | string separator d at
  • the end deletes
  • p at the end prints
  • g at the end (how do you use d and p) changes them all The occurrences

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 <br></br>buy rice <br></br>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 <br></br>s/carla/uira/ <br></br><p>s/</p><p align="center">/
</p>
$ 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.

Did you like it?

Share