我是Linux和Bash的新手。
我正在尝试用另一条替换一行,但我不知道如何正确使用sed:
旧行:
"certificate_path": "/etc/ajenti/ajenti.pem"
新队:
"certificate_path": "/etc/nginx/ssl/xyz-combined.pem"
我试过了:
sed -i '26s/.*//etc/nginx/ssl/xyz.de-combined.pem"/' /etc/ajenti/config.json
最好的问候,Aeris
结合使用sed和backreference:
sed -i 's~\("certificate_path": "/etc/\)ajenti/ajenti.pem"~\1ssl/xyz-combined.pem"~' /etc/ajenti/config.json
新旧字符串的共同部分用方括号("certificate_path": "/etc/
)捕获,并使用\1
对新路径的反向引用进行前缀nginx/ssl/xyz-combined.pem"
。
编辑:
要在替换字符串中使用变量,只需将变量名称括在单引号中即可进行参数扩展:
mydomain="xyz-combined.pem"
sed -i 's~\("certificate_path": "/etc/\)ajenti/ajenti.pem"~\1ssl/'$mydomain'"~' /etc/ajenti/config.json
您还可以用双引号将sed命令括起来,并转义所有内部双引号:
sed -i "s~\(\"certificate_path\": \"/etc/\)ajenti/ajenti.pem\"~\1ssl/$mydomain\"~" /etc/ajenti/config.json
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句