Categories: How-To

sed command on Linux (alters texts) [Basic Guide]

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 
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.

Did you like it?

Share

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Share
Published by
Uirá Endy Ribeiro

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.