本文介绍了如何从Windows命令提示符中将OR运算符与命令FINDSTR一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Findstr 应该支持正则表达式,而我使用它的方式我需要使用 OR 来检查文件是否以 .exe结尾./code> .dll .但是,我无法执行 OR 操作.Windows考虑使用 | 尝试传递上一个命令,并且将 OR 读为文字 OR .

Findstr is supposed to support regular expressions and the way I am using it I need to have an OR to check if a file ends in .exe OR .dll. However I cannot get the OR operation to work. Windows thinks on using | that I try to pipe the previous command and OR is read as literal OR.

推荐答案

findstr.exe 仅支持非常有限的一组正则表达式字符.在命令提示符窗口中运行 findstr/?会导致显示此控制台应用程序的帮助,并列出受支持的正则表达式字符及其含义.

findstr.exe in Windows system32 directory supports only a very limited set of regular expression characters. Running in a command prompt window findstr /? results in getting displayed help for this console application listing also the supported regular expression characters with their meanings.

但是,正如 Eryk Sun 在上面的评论中所述,可以在命令行中指定多个搜索字符串以构建简单的 OR 表达式.

But as Eryk Sun explained in his comment above, multiple search strings can be specified on command line to build a simple OR expression.

如果有一个列表文件 FileNames.lst 包含例如

In case of having a list file FileNames.lst containing for example

C:\Program Files\Internet Explorer\ieproxy.dll
C:\Program Files\Internet Explorer\iexplore.exe
C:\Program Files\Internet Explorer\iexplore.exe.mui

,仅所有以 .dll OR 结尾的文件名(不区分大小写)应通过命令 findstr 来输出.strong>,获取此输出的命令行可能是:

and just all file names ending with .dll OR .exe case-insensitive should be output by command findstr, the command line for getting this output could be:

%SystemRoot%\system32\findstr.exe /I /R "\.exe$ \.dll$" FileNames.lst

输出用于 FileNames.lst 中的示例行:

C:\Program Files\Internet Explorer\ieproxy.dll
C:\Program Files\Internet Explorer\iexplore.exe

findstr 将正则表达式搜索字符串中的空格解释为两个字符串之间的分隔符.因此, findstr 使用正则表达式字符串 \.dll $ \.exe $ 搜索,并返回两个表达式之一与a匹配的所有行.字符串.

The space in regular expression search string is interpreted by findstr as a separator between the two strings. Therefore findstr searches with the regular expression strings \.dll$ and \.exe$ and returns all lines where one of the two expressions matches a string.

另一个正则表达式字符串的方法是在命令行上多次使用参数/C:"..." ,这在以下情况下是必需的正则表达式搜索字符串包含一个或多个空格,应在搜索表达式中将其作为文字字符包含.

Another method to OR two or more regular expression strings would be using parameter /C:"..." multiple times on command line which is necessary when a regular expression search string contains one or more spaces which should be included as literal character(s) in search expression.

%SystemRoot%\system32\findstr.exe /I /R /C:"\.dll$" /C:"\.exe$" FileNames.lst

结果与上面的其他命令行相同.

The result is the same as above with the other command line.

但是对于此特定任务,根本不需要运行正则表达式搜索,因为 findstr 还提供了参数/E ,用于仅返回搜索到的字符串所在的行在一行的末尾.

But for this specific task it is not necessary at all to run a regular expression search as findstr offers also the parameter /E for returning only lines where the searched strings are found at end of a line.

%SystemRoot%\system32\findstr.exe /E /I /C:.exe /C:.dll FileNames.lst

使用''...... /C:``...'':

  1. "regexp1 regexp2 regexp3" 表示搜索具有由三个空格分隔的正则表达式之一匹配的字符串的行.选项/R 可以额外用于将两个空格之间的三个字符串显式解释为正则表达式.建议这样做是为了使 findstr 和每个阅读器都100%清楚地将搜索字符串解释为正则表达式.
  2. /L"word1 word2 word3" 表示搜索带有字符串的行,该字符串与三个用空格分隔的字面解释的字符串之一匹配.使用的选项/L 强制将两个空格之间的三个字符串解释为文字字符串,而不是正则表达式.
  3. /C:单词1"/C:单词2"表示/C:单词3" 表示搜索的字符串中包含与三个字面解释的字符串之一相匹配的字符串,在该字符串上将空格字符解释为空格.可以另外使用选项/L 将三个搜索字符串明确地解释为文字字符串.建议这样做是为了使 findstr 和每个读者都将搜索字符串解释为文字字符串100%清除.
  4. /R/C:"reg exp 1"/C:"reg exp 2"/C:"reg exp 3" 表示搜索具有与三个正则表达式字符串之一匹配的字符串的行,在该三个正则表达式字符串中将空格字符解释为空格.选项/R 强制将三个字符串显式解释为正则表达式,而空格则解释为空格.
  1. "regexp1 regexp2 regexp3" means searching for a line with a string matched by one of the three space separated regular expressions. The option /R can be used additionally to explicitly interpret the three strings between the two spaces as regular expressions. It is advisable to do so for making it 100% clear for findstr and every reader that the search strings are interpreted as regular expressions.
  2. /L "word1 word2 word3" means searching for a line with a string matched by one of the three space separated literally interpreted strings. The used option /L forces explicitly an interpretation of the three strings between the two spaces as literal strings and not as regular expressions.
  3. /C:"word 1" /C:"word 2" /C:"word 3" means searching for a line with a string matched by one of the three literally interpreted strings on which the space character is interpreted as space. The option /L can be used additionally to explicitly interpret the three search strings between as literal strings. It is advisable to do so for making it 100% clear for findstr and every reader that the search strings are interpreted as literal strings.
  4. /R /C:"reg exp 1" /C:"reg exp 2" /C:"reg exp 3" means searching for a line with a string matched by one of the three regular expressions strings on which the space character is interpreted as space. The option /R forces explicitly an interpretation of the three strings as regular expressions with space being interpreted as space.

这篇关于如何从Windows命令提示符中将OR运算符与命令FINDSTR一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:20