bash 在grep中转义斜杠“\”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44034124/
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
Escaping slash "\" in grep
提问by Mahesha999
I have file with line:
我有一行文件:
"H:\Check\WP_20140511_029.mp4"
along with other lines. I want to remove such lines indicating directory at H:\Check
. I tried
与其他线路一起。我想删除这些指示目录的行H:\Check
。我试过
grep -v ".*H:\Check.*" testout.txt > testout2.txt
But it did not delete those lines. Whats wrong with my regex .*H:\\Check.*
.
regex101 showsthat my regex correctly matches the line.
但它并没有删除那些行。我的正则表达式有什么问题.*H:\\Check.*
。
regex101 显示我的正则表达式正确匹配该行。
回答by anubhava
You can use:
您可以使用:
grep -v 'H:\Check' testout.txt > testout2.txt
It is important to use single quotes to avoid excessive escaping of backslash.
使用单引号以避免过度转义反斜杠很重要。
Using double quotes equivalent command will be this:
使用双引号等效命令将是这样的:
grep -v "H:\\Check" testout.txt > testout2.txt
EDIT:
编辑:
\\
in double quotes is equivalent of a single backward slash due to shell expansion that happens in double quotes only. It is evident from these echo
commands:
\\
由于仅在双引号中发生的外壳扩展,双引号中的 等效于单个反斜杠。从这些echo
命令可以看出:
echo "H:\Check"
H:\Check
echo 'H:\Check'
H:\Check
回答by Davy M
First off, when using grep, you don't need to specify the any characters .* combination. Grep is already built to look for your phrase (Example grepping for he in a file that has a line that says Hi there
will return the full line without the need to use .*he.*
as your regex does)
首先,在使用 grep 时,您不需要指定任何字符 .* 组合。Grep 已经构建用于查找您的短语(例如,在一个文件中为 he 进行 grepping,该文件中的一行表示Hi there
将返回整行,而无需.*he.*
像您的正则表达式那样使用)
Second (And I see anubhava beat me to the punch on this one as I was typing), the backslash escaping works differently depending on your use of quotation marks.
其次(我在打字时看到 anubhava 在这个问题上打了我一拳),反斜杠转义的工作方式不同,具体取决于您对引号的使用。
If you are using single quotes, the single backslash will escape the second, so you need two, so your regex will be H:\\Check
.
如果您使用单引号,则单个反斜杠将转义第二个,因此您需要两个,因此您的正则表达式将为H:\\Check
.
If you are using double quotes (As you are), the backslash will escape the other backslash while the quotes are evaluated, then the resulting backslash will be trying to escape the C (which isn't even close to what we want), so you actually need a third backslash so that they escape correctly.
如果您使用双引号(就像您一样),反斜杠将在评估引号时转义另一个反斜杠,然后产生的反斜杠将试图转义 C(它甚至不接近我们想要的),所以您实际上需要第三个反斜杠,以便它们正确转义。
If you wanted to do it without quotes (which in my opinion isn't very good style, but since you have a single phrase you could if you really wanted to), you would actually need 4 backslashes for a similar reason. The \ would normally let the command continue to the next line, so the first escapes the second, the third escapes the 4th, then there are only 2 when grep is evaluated, which is the escaped correctly.
如果你想不加引号(在我看来这不是很好的风格,但因为你有一个短语,如果你真的想的话,你可以这样做),出于类似的原因,你实际上需要 4 个反斜杠。\ 通常会让命令继续到下一行,所以第一个转义第二个,第三个转义第四个,然后在评估 grep 时只有 2 个,这是正确转义的。
You should try playing around with Grep some more to see what it's capable of, it can do basically any searching you ever want (and much more that you'll never want) as long as you know the syntax.
您应该尝试更多地使用 Grep 以了解它的功能,只要您知道语法,它基本上可以执行您想要的任何搜索(以及更多您永远不会想要的搜索)。
Edit: Fixed a little syntax, thanks. I didn't know about that * converts text to italics on these answers.
编辑:修复了一些语法,谢谢。我不知道 * 在这些答案上将文本转换为斜体。