Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add string with backslash in file using sed command

I need to add a text string:

--with-mpm=event \

to httpd.spec on 138th position.

I tried:

sed -i '138i--with-mpm=event \\' /root/rpmbuild/SPECS/httpd.spec

This code runs in bash script of Vagrantfile during bootstrapping virtual machine. However, the script returns error.

When I check httpd.spec, output is a string missing backslash:

--with-mpm=event 

It works okay running it directly in shell of virtual machine though.

How can I fix it with sed?

Thanks!

like image 942
ERemarque Avatar asked May 24 '26 20:05

ERemarque


1 Answers

The rule of thumb when dealing with backslashes is keep adding up backslashes until you get the expected result.

In this case, a literal backslash here needs to be coded with four backslashes:

sed -i '138i--with-mpm=event \\\\' /root/rpmbuild/SPECS/httpd.spec
like image 82
Wiktor Stribiżew Avatar answered May 26 '26 17:05

Wiktor Stribiżew