如何使用GNU grep for windows / dos转义双引号? [英] How do I escape a double quote with GNU grep for windows/dos?

查看:256
本文介绍了如何使用GNU grep for windows / dos转义双引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在所有档案中搜寻



Assembly =Foobar



输出到tmp.txt



我试过了(cmd / error)


grep -rAssembly = \Foobar。 >> tmp.txt
错误(无效参数)



grep -rAssembly =Foobar。 >> tmp.txt

错误(空间不足)



grep -r'Assembly =Foobar。>> tmp.txt

错误(空间不足)


非常令人沮丧。你需要担心grep和cmd.exe的转义引号



grep需要转义引号 \



cmd.exe以 ^。引用是一个状态机,意味着未转义的引号开启和关闭引号语义当引用时,特殊字符如< | & 等被视为字面量,第一个非转义引用打开引号语义,下一个引号将引用语义关闭。因此在你的情况下,>> tmp.txt 不会被处理



以下任何一种都可以工作:

  grep -rAssembly = \Foobar ^。 >> tmp.txt 
grep -r ^Assembly = \Foobar。> tmp.txt
grep -r ^Assembly = \ ^Foobar ^。 >>>> tmp.txt
grep -r Assembly = \ ^Foobar。> tmp.txt

如果您的搜索字词包含空格,则grep需要包含引号,因此您需要前三个形式中的任何一个。最后一个形式不适用于搜索字词中的空格。


I'm trying to search for

Assembly="Foobar

in all files and direct the output to tmp.txt

I've tried (cmd/error)

grep -r "Assembly=\"Foobar" . >> tmp.txt error (invalid argument)

grep -r "Assembly=""Foobar" . >> tmp.txt
error (not enough space)

grep -r 'Assembly="Foobar' . >> tmp.txt
error (not enough space)

very frustrating. Any suggestions?

解决方案

You need to worry about escaping quotes for both grep and cmd.exe

grep expects quotes to be escaped as \"

cmd.exe escapes quotes as ^". The quote is a state machine, meaning unescaped quotes toggle quote semantics on and off. When quoting is on, special characters like <, |, &, etc are treated as literals. The first unescaped quote turns on quote semantics. The next quote turns quote semantics off. It is only possible to escape a quote when quoting is OFF. Unbalanced quotes result in the remainder being quoted. So in your case, the >>tmp.txt is not treated as redirection by cmd.exe, so it gets passed to grep as an argument.

Any of the following will work:

grep -r "Assembly=\"Foobar^" . >>tmp.txt
grep -r ^"Assembly=\"Foobar" . >>tmp.txt
grep -r ^"Assembly=\^"Foobar^" . >>tmp.txt
grep -r Assembly=\^"Foobar . >>tmp.txt

If your search term included spaces, then grep needs the enclosing quotes, so you would need any of the first three forms. The last form would not work with spaces in the search term.

这篇关于如何使用GNU grep for windows / dos转义双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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