Linux 在grep中转义双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11995353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:06:11  来源:igfitidea点击:

Escape double quote in grep

linuxshell

提问by Qiang Li

I wanted to do grep for keywords with double quotes inside. To give a simple example:

我想为里面有双引号的关键字做 grep 。举个简单的例子:

echo "member":"time" | grep -e "member\""

That does not match. How can I fix it?

那不匹配。我该如何解决?

采纳答案by cmh

The problem is that you aren't correctly escaping the input string, try:

问题是您没有正确转义输入字符串,请尝试:

echo "\"member\":\"time\"" | grep -e "member\""

Alternatively, you can use unescaped double quotes within single quotes:

或者,您可以在单引号中使用未转义的双引号:

echo '"member":"time"' | grep -e 'member"'

It's a matter of preference which you find clearer, although the second approach prevents you from nesting your command within another set of single quotes (e.g. ssh 'cmd').

这是您发现更清楚的偏好问题,尽管第二种方法可以防止您将命令嵌套在另一组单引号中(例如ssh 'cmd')。