简体   繁体   中英

sed replace and keep double quotes

I just want to change the server and IP information inside double quotes.

For Example: original line:

"server":"10.10.10.10:3306"

OR

"server":"localhost:3306"

output line:

"server":"20.20.20.20:3306"

I tried using...

sed -i s/.*/\"server\"\:\"20\.20\.20\.20\:3306\"/

output:

server:20.20.20.20:3306

where all double quoest gone? it replaces the values but does not keep double quotes. I want it should not change double quotes.

Any help?

You need to enclose the sed pattern within single quotes. If you do like that, then you don't need to escape the double quotes.

$ echo '"server":"10.10.10.10:3306"' | sed 's/.*/"server":"20.20.20.20:3306"/'
"server":"20.20.20.20:3306"

OR

You could simply do like this,

$ echo '"server":"10.10.10.10:3306"' | sed 's/[^":]\+:/20.20.20.20:/'
"server":"20.20.20.20:3306"
$ echo '"server":"localhost:3306"' | sed 's/[^":]\+:/20.20.20.20:/'
"server":"20.20.20.20:3306"

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