如何在 sed 中转义双引号和单引号? [英] How do I escape double and single quotes in sed?

查看:55
本文介绍了如何在 sed 中转义双引号和单引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,当您使用单引号时,里面的所有内容都被视为文字.我想要那个作为我的替代品.但我也想找到一个带有单引号或双引号的字符串.

例如

sed -i 's/"http://www.fubar.com"/URL_FUBAR/g'

我想用 URL_FUBAR 替换http://www.fubar.com".sed 应该如何识别我的//或我的双引号?

感谢您的帮助!

我可以使用 s/"http://www.fubar.com"/URL_FUBAR/g 吗?>

真的会转义单引号内的字符吗?

解决方案

sed 中的 s/// 命令允许您使用其他字符代替 / 作为分隔符,如

sed 's#"http://www.fubar.com"#URL_FUBAR#g'

sed 's,"http://www.fubar.com",URL_FUBAR,g'

双引号不是问题.要匹配单引号,请切换两种类型的引号.请注意,单引号字符串可能不包含单引号(甚至不包含转义的).

如果 sed 要将它们解释为文字点而不是正则表达式模式 . 匹配任何一个字符,则需要对点进行转义.

From what I can find, when you use single quotes everything inside is considered literal. I want that for my substitution. But I also want to find a string that has single or double quotes.

For example,

sed -i 's/"http://www.fubar.com"/URL_FUBAR/g'

I want to replace "http://www.fubar.com" with URL_FUBAR. How is sed supposed to recognize my // or my double quotes?

Thanks for any help!

EDIT: Could I use s/"http://www.fubar.com"/URL_FUBAR/g ?

Does actually escape chars inside the single quotes?

解决方案

The s/// command in sed allows you to use other characters instead of / as the delimiter, as in

sed 's#"http://www.fubar.com"#URL_FUBAR#g'

or

sed 's,"http://www.fubar.com",URL_FUBAR,g'

The double quotes are not a problem. For matching single quotes, switch the two types of quotes around. Note that a single quoted string may not contain single quotes (not even escaped ones).

The dots need to be escaped if sed is to interpret them as literal dots and not as the regular expression pattern . which matches any one character.

这篇关于如何在 sed 中转义双引号和单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆