在findstr搜索字符串中转义报价 [英] Escaping a quote in findstr search string

查看:211
本文介绍了在findstr搜索字符串中转义报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



示例:



pre> findstr / misc:namespace = \* .cs> ns.txt

这个输出到控制台,而不是我指定的文件。



我直接在命令行上这样做,实际上在批处理文件中,虽然该信息也可能有用。

解决方案

如果我错了,请更正我,但我认为我已经弄清楚了:

  findstr.exe / misc:^namespace = \ ^^* .cs > ns.txt 

即使您的搜索字符串中有空格,这似乎也提供了正确的输出。它允许文件重定向,管道和附加文字在同一个findstr.exe调用中正常工作。



我的问题中的原始命令不起作用,因为两个cmd。 exe和findstr.exe对

我的答案中的新命令有效,因为 ^允许报价从cmd.exe传递到findstr.exe,而 \告诉findstr.exe忽略该引用的命令处理目的,并将其视为字符文字。



编辑



嗯,我的解决方案是对的,但是正确的原因是完全错误的,我写了一个小程序来测试它。

我发现当我传递错误的命令行时,cmd.exe将此输入传递给程序:

  test.exe / misc:namespace =* .cs> ns.txt 

如果字符转义正确,cmd.exe将此输入传递给程序(并重定向输出到文件):

  test.exe / misc:namespace =* .cs 


How can I properly escape a quote in a search string when using findstr.exe?

Example:

findstr /misc:"namespace=\"" *.cs > ns.txt

This outputs to the console, instead of to the file I specified.

I am doing this directly on the command line, not actually in a batch file, though that information might be useful too.

解决方案

Please correct me if I'm wrong, but I think I've figured it out:

findstr.exe /misc:^"namespace=\^"^" *.cs > ns.txt

This seems to give the correct output, even if you have spaces in your search string. It allows file redirection, piping, and additional literals in the same findstr.exe invocation to work correctly.

The original command in my question doesn't work because both cmd.exe and findstr.exe have special processing for the " character. I ended up with an unmatched set of quotes in cmd.exe's processing.

The new command in my answer works because ^" allows the quote to pass from cmd.exe to findstr.exe, and \" tells findstr.exe to ignore that quote for command processing purposes, and treat it as a character literal.

Edit:

Well, my solution was right, but the reason it is correct was totally wrong. I wrote a small program to test it.

I found out that cmd.exe passes this input to the program when I pass the bad command line:

test.exe /misc:namespace=" *.cs > ns.txt

With the characters escaped correctly, cmd.exe passes this input to the program (and redirects output to a file):

test.exe /misc:namespace=" *.cs

这篇关于在findstr搜索字符串中转义报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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