Aeris Aeris - 4 months ago 22
Linux Question

Sed replacing string with quotation marks

I'm kinda new to Linux and Bash.

I'm trying to replace a line with another one and i don't know how to use sed right:

old line:

"certificate_path": "/etc/ajenti/ajenti.pem"


new line:

"certificate_path": "/etc/nginx/ssl/xyz-combined.pem"


I tried:

sed -i '26s/.*/\/etc\/nginx\/ssl\/xyz.de-combined.pem"/' /etc/ajenti/config.json


best regards,
Aeris

Answer

Using sed with backreference:

sed -i 's~\("certificate_path": "/etc/\)ajenti/ajenti.pem"~\1ssl/xyz-combined.pem"~' /etc/ajenti/config.json

The common part of old and new string is captured with brackets ("certificate_path": "/etc/) and prepended using backreference \1 to the new path : nginx/ssl/xyz-combined.pem".

Comments