简体   繁体   中英

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'". 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. PS sed is from openwrt busybox, limited, supports only 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.

If the "EST-10" part contained spaces, then you would need to put it into single quotes too:

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 ! , that then need escaping.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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