Aeris Aeris - 1 year ago 128
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\/"/' /etc/ajenti/config.json

best regards,

Answer Source

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".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download