Insert text with sed on the same line as pattern search

For example, if I have this file:

The green horse is blue localhost
my pants is dirty localhost
your eyes is so beautiful localhost
The best area is localhost

And I want to add text (192.168.25.50) to this file after each search for the "localhost" template.

  • I do not want to create a new line
  • I do not want to insert each end of the line, but right after the pattern
  • I don't want to use awk, I need to know if the sed command can do this.

In fact, my result should look like this:

The green horse is blue localhost 192.168.25.50
my pants is dirty localhost 192.168.25.50
your eyes is so beautiful localhost 192.168.25.50
The best area is localhost 192.168.25.50

There are many sed problems, but everyone has a specific context that cannot help my simple and fundamental problem. I can already insert the text after the template into one file, but never on one line.

Can you explain to me how to embed the sed command on the same line as the template used by the sed command itself?

+7
2

GNU sed:

sed 's/\blocalhost\b/& 192.168.25.50/' file

\b:

&: ,

" ", sed -i.

+16

, , , , , ABC/*. H *

0

Source: https://habr.com/ru/post/1627643/


All Articles