1

I am trying to replace a string with sed (stream editor), however I am getting errors thrown back. Probably because I am not using the string correctly, because of the symbols.

I am trying to replace the following string:

<VirtualHost |IP|:|PORT_80| |MULTI_IP|> 

with

<VirtualHost 127.0.0.1:8080 |IP|:8080 |MULTI_IP|>

In the file called virtual_host2.conf

Buw when I try several things, I always get errors back like:

sed: -e expression #1, char 90: unterminated `s' command
sed: -e expression #1, char 20: unknown option to `s'

I am not that experienced with sed, but I can replace text strings normally. But I think because of the symbols in the current string, like |, < and > it doesn't work like I would.

Can someone please give me a working example on how to replace the above? Or point me in the right direction.

I tried the following myself:

sed -i 's<VirtualHost |IP|:|PORT_80| |MULTI_IP|>/<VirtualHost 127.0.0.1:8080 |IP|:8080 |MULTI_IP|>' /virtual_host2.conf

sed -i 's|<VirtualHost |IP|:|PORT_80| |MULTI_IP|>|<VirtualHost 127.0.0.1:8080 |IP|:8080 |MULTI_IP|>|g' /virtual_host2.conf

But both give the error as mentioned above.

Any idea?

1 Answer 1

2

You omit / in sed 's///' pattern. Try this:

sed 's/<VirtualHost |IP|:|PORT_80| |MULTI_IP|>/<VirtualHost 127.0.0.1:8080 |IP|:8080 |MULTI_IP|>/' /virtual_host2.conf

And if everything is ok, add -i option to execut real replacement in file.

g option in 's///g' pattern perform global replacement of all occurances, not the first in each line.


You can use some other symbols as delimeter, such as 's|||', but it not make sense in your example because you need to quote each | symbol. This command will get the same output:

sed 's|<VirtualHost \|IP\|:\|PORT_80\| \|MULTI_IP\|>|<VirtualHost 127.0.0.1:8080 \|IP\|:8080 \|MULTI_IP\|>|' /virtual_host2.conf
2
  • Thank you for your help and details. I didn't even know if I used sed without "i" that it would show the changes. Thank you kindly. Learned a new thing today.
    – Restafval
    Oct 24, 2017 at 7:47
  • @Restafval, great! See this tutorial for more tricks with sed: grymoire.com/Unix/Sed.html Oct 24, 2017 at 7:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .