我正在尝试从目录树中的文件中获取带有单引号或双引号的行。 举个例子,我想用一个findtr命令来获取这些行:
You should be able to get a "Hello world" program really quick. Enter a value for params 'name' and 'age', please. If no value is entered for 'name' param, the "Hello world" program will throw an exception.我只能使用单引号( findstr /srn \' *.txt ),只能使用双引号( findstr /srn \^" *.txt ),或单引号和双引号( findstr /srn \'\^" *.txt ),但我需要只有一个命令的带单引号或双引号的行。
有关如何实现它的任何想法?
I'm trying to get lines from files in a directory tree with a single or double quote in them. As an example, I want to get these lines with a single findstr command:
You should be able to get a "Hello world" program really quick. Enter a value for params 'name' and 'age', please. If no value is entered for 'name' param, the "Hello world" program will throw an exception.I can get lines with only single quotes (findstr /srn \' *.txt), only double quotes (findstr /srn \^" *.txt), or both single and double quotes (findstr /srn \'\^" *.txt), but I need lines with single or double quotes with only a single command.
Any idea about how to achieve it?
最满意答案
爆炸丸有正确的想法,但没有正确的语法。
当试图逃避FINDSTR和CMD解析器的引用时,它会变得棘手。 你想要的正则表达式是['\"] ,但是你需要逃避"为CMD解析器。
这将有效:
findstr /srn ['\^"] *.txt这样的话
findstr /srn "['\"]^" *.txt这样的话
findstr /srn ^"['\^"]^" *.txt注意 由于一个令人讨厌的FINDSTR错误,您有可能无法找到包含该字符串的文件。 如果启用了8.3短名称且文件夹包含扩展名超过3个以.txt开头的字符的文件,则/S选项可能无法找到文件。 (例如name.txt2 )。 有关详细信息,请参阅标记为BUG的部分- 短8.3文件名可以打破/ D和/ S选项 Windows FINDSTR命令的未记录的功能和限制是什么? 。
Explosion Pills had the correct idea, but not the correct syntax.
It can get tricky when trying to escape the quote for both FINDSTR and for the CMD parser. The regex you want is ['\"], but then you need to escape the " for the CMD parser.
This will work:
findstr /srn ['\^"] *.txtand so will this
findstr /srn "['\"]^" *.txtand so will this
findstr /srn ^"['\^"]^" *.txtNOTE You are at risk of failing to find files that contain the string because of a nasty FINDSTR bug. The /S option may fail to find files if 8.3 short names are enabled and a folder contains a file with an extension longer than 3 chars that starts with .txt. (name.txt2 for example). For more information, see the section labeled BUG - Short 8.3 filenames can break the /D and /S options at What are the undocumented features and limitations of the Windows FINDSTR command?.
更多推荐
发布评论