简体   繁体   English

用sed用单引号替换行

[英]replace line with single quotes by sed

I want to change single line of config started with "option timezone" to a line with single quotes: "option timezone 'EST-10'". 我想将以“ option timezone”开始的单行配置更改为单引号:“ option timezone'EST-10'”。 However when I do this 但是当我这样做时

sed -i '/option timezone/c\option timezone 'EST-10'' /etc/config/system

single quotes missed and result is like this: 缺少单引号,结果是这样的:

head /etc/config/system 

config system
option timezone EST-10

Of course backslash before quotes doesn't help. 当然,引号前的反斜杠无济于事。 Can I achieve it somehow with \\c command. 我可以用\\ c命令以某种方式实现它吗? PS sed is from openwrt busybox, limited, supports only e,f,i,n,r. PS sed来自openwrt busybox,有限,仅支持e,f,i,n,r。

Try this: 尝试这个:

sed -i '/option timezone/c\option timezone '\'EST-10\' /etc/config/system

Adjacent strings are automatically concatenated by bash, so this closes the first string, adds a single quote (which needs to be escaped), EST-10, then another escaped single quote. 相邻的字符串由bash自动连接,因此将关闭第一个字符串,添加一个单引号(需要转义),EST-10,然后添加另一个转义的单引号。

If the "EST-10" part contained spaces, then you would need to put it into single quotes too: 如果“ EST-10”部分包含空格,那么您也需要将其放在单引号中:

sed -i '/option timezone/c\option timezone '\''EST - 10'\' /etc/config/system

Double quotes are also an option but personally I prefer not to use them as there are a whole load of other characters that Bash will interpret, such as $ and ! 双引号也是一种选择,但我个人不希望使用双引号,因为Bash会解释很多其他字符,例如$! , that then need escaping. ,那么就需要转义。

您可以在双引号sed命令中使用单引号字符串,而不必费心将它们转义:

sed -i "/option timezone/c\option timezone 'EST-10'" /etc/config/system

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM