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"
.
Edit:
To use a variable in the replacement string, just surround the variable name with single quotes to allow parameter expansion:
mydomain="xyz-combined.pem"
sed -i 's~\("certificate_path": "/etc/\)ajenti/ajenti.pem"~\1ssl/'$mydomain'"~' /etc/ajenti/config.json
You can also surround the sed command with double quotes and escape all internal double quotes :
sed -i "s~\(\"certificate_path\": \"/etc/\)ajenti/ajenti.pem\"~\1ssl/$mydomain\"~" /etc/ajenti/config.json