如何使用FINDSTR获取带有简单OR双引号的行(How to use FINDSTR to get lines with simple OR double quotes)

编程入门 行业动态 更新时间:2024-10-27 01:31:47
如何使用FINDSTR获取带有简单OR双引号的行(How to use FINDSTR to get lines with simple OR double quotes)

我正在尝试从目录树中的文件中获取带有单引号或双引号的行。 举个例子,我想用一个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 ['\^"] *.txt

and so will this

findstr /srn "['\"]^" *.txt

and so will this

findstr /srn ^"['\^"]^" *.txt

NOTE 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?.

更多推荐

本文发布于:2023-08-06 11:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448857.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   简单   双引号   FINDSTR   quotes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!