"sed" command in bash

113,626

Solution 1

sed is the Stream EDitor. It can do a whole pile of really cool things, but the most common is text replacement.

The s,%,$,g part of the command line is the sed command to execute. The s stands for substitute, the , characters are delimiters (other characters can be used; /, : and @ are popular). The % is the pattern to match (here a literal percent sign) and the $ is the second pattern to match (here a literal dollar sign). The g at the end means to globally replace on each line (otherwise it would only update the first match).

Solution 2

Here sed is replacing all occurrences of % with $ in its standard input.

As an example

$ echo 'foo%bar%' | sed -e 's,%,$,g'

will produce "foo$bar$".

Solution 3

It reads Hello World (cat), replaces all (g) occurrences of % by $ and (over)writes it to /etc/init.d/dropbox as root.

Solution 4

sed is a stream editor. I would say try man sed.If you didn't find this man page in your system refer this URL:

http://unixhelp.ed.ac.uk/CGI/man-cgi?sed

Share:
113,626
ajsie
Author by

ajsie

please delete me

Updated on October 17, 2020

Comments

  • ajsie
    ajsie over 3 years

    Could someone explain this command for me:

    cat | sed -e 's,%,$,g' | sudo tee /etc/init.d/dropbox << EOF
       echo "Hello World"
    EOF
    

    What does the "sed" command do?

  • Programster
    Programster over 10 years
    For some reason sed is replacing all lines for me, not just the first when I leave out the g at the end. e.g. sudo sed -i 's,enabled=0,enabled=1,' /etc/yum.repos.d/epel.repo in an amazon AWS image with sed version 4.2.1
  • Jack Kelly
    Jack Kelly over 10 years
    sed will always run on each line. The g option makes it replace every match on that line instead of the first match on that line.