Windows递归grep命令行

244

我需要在Windows中进行递归grep,类似于Unix/Linux中的以下操作:

grep -i 'string' `find . -print`

或者更受欢迎的方法:
find . -print | xargs grep -i 'string'

我只能使用cmd.exe,因此只能使用Windows内置命令。不幸的是,我无法在此服务器上安装Cygwin或任何第三方工具,例如UnxUtils。我甚至不确定是否可以安装PowerShell。有没有仅使用cmd.exe内置命令(Windows 2003 Server)的建议?


1
没有 PowerShell 确实有些困难,为什么不能安装呢? - Chris Ballance
1
系统管理员正在限制我们服务器的权限。如果有人有任何PowerShell建议,请提出来,我会看看我们是否可以安装PowerShell。 - Andy White
4
顺便说一下,在Linux中最好这样写:"find . |xargs grep -i string"。区别在于,如果find命令返回的列表非常长,您可能会超出最大命令长度(我曾经遇到过这种情况),从而无法进行grep操作。使用xargs可以使每个找到的文件只调用一次grep。 - Nathan Fellman
2
许多版本的Grep,包括Gnu Grep,在内部提供了递归搜索功能(http://www.gnu.org/software/grep/manual/grep.html#File-and-Directory-Selection),因此您的搜索可以编写为`grep -i 'string' -R .`,就像@NathanFellman建议的那样,避免了过长命令的问题。 - Scott Centoni
9个回答

281

findstr可以进行递归搜索(/S),并支持一些正则表达式语法的变体(/R)。

C:\>findstr /?
Searches for strings in files.

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ ...]]

  /B         Matches pattern if at the beginning of a line.
  /E         Matches pattern if at the end of a line.
  /L         Uses search strings literally.
  /R         Uses search strings as regular expressions.
  /S         Searches for matching files in the current directory and all
             subdirectories.
  /I         Specifies that the search is not to be case-sensitive.
  /X         Prints lines that match exactly.
  /V         Prints only lines that do not contain a match.
  /N         Prints the line number before each line that matches.
  /M         Prints only the filename if a file contains a match.
  /O         Prints character offset before each matching line.
  /P         Skip files with non-printable characters.
  /OFF[LINE] Do not skip files with offline attribute set.
  /A:attr    Specifies color attribute with two hex digits. See "color /?"
  /F:file    Reads file list from the specified file(/ stands for console).
  /C:string  Uses specified string as a literal search string.
  /G:file    Gets search strings from the specified file(/ stands for console).
  /D:dir     Search a semicolon delimited list of directories
  strings    Text to be searched for.
  [drive:][path]filename
             Specifies a file or files to search.

Use spaces to separate multiple search strings unless the argument is prefixed
with /C.  For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y.  'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.

Regular expression quick reference:
  .        Wildcard: any character
  *        Repeat: zero or more occurrences of previous character or class
  ^        Line position: beginning of line
  $        Line position: end of line
  [class]  Character class: any one character in set
  [^class] Inverse class: any one character not in set
  [x-y]    Range: any characters within the specified range
  \x       Escape: literal use of metacharacter x
  \<xyz    Word position: beginning of word
  xyz\>    Word position: end of word

For full information on FINDSTR regular expressions refer to the online Command
Reference.

52
findstr是grep的一个很好的替代品。我倾向于使用findstr /sinp(递归、忽略大小写、跳过二进制文件,并显示行号)。 - Steve Rowe
7
很遗憾,根据文档和我尝试使用的模式来看,findstr 对正则表达式的支持非常有限。 - John Kaster
4
叹口气,相信微软会增加一个新的实用程序(findstr),而不是修复现有的一个(find)。如果你希望findstr能够计算行数,请使用以下命令 -- findstr [选项] | find /c /v "" -- 使用findstr匹配行并使用find计算它们的数量。是的,find认为空字符串没有匹配的行,所以使用/v选项时每一行都会匹配。 - yoyo
6
他们不想打破依赖于find错误行为的现有管道。 - i_am_jorf

176
findstr /spin /c:"string" [files]

参数的含义如下:

  • s = 递归搜索子目录
  • p = 跳过不可打印字符
  • i = 大小写不敏感
  • n = 输出行号

要搜索的字符串是你在/c:后面用引号括起来的部分。


3
抱歉,你能否举一个例子?“spin”是什么意思?它是要查找的文本行吗?不是应该用/g或/f来指定文件吗?那方括号又是什么意思? - Wolfpack'08
7
findstr /? 解释了每个参数。s = 递归,p = 跳过非可打印字符,i = 不区分大小写,n = 打印行号。您不一定需要所有这些,但我喜欢它们,而且 'spin' 很容易记住。要搜索的字符串是在 /c: 后面用引号括起来的部分。 - i_am_jorf
6
哦哈哈,我打了一个 /?,但我其实不知道修改器是像 /spin 这样使用的。我以为它们是像 /s/p/i/n 这样使用的。 - Wolfpack'08
5
好的,一般而言,有些命令行程序在输入“/”时可以稍微放松一下。这个程序就是其中之一。但并非所有命令行程序都支持此操作。你知道,命令行很特殊。 - i_am_jorf
3
然而,它作为Windows的一部分进行分发,因此符合原帖发布者的要求,可以使用cmd.exe完成而不需要安装任何其他软件。 - i_am_jorf
显示剩余3条评论

41
我刚刚使用以下命令搜索了一个文本,它列出了所有包含我指定的“搜索文本”的文件名。
C:\Users\ak47\Desktop\trunk>findstr /S /I /M /C:"search text" *.*

感谢您提供的唯一一个简单易用的示例。 - Nav

28
在“src”文件夹中进行递归搜索“import”关键字:
findstr /s import .\src\*

15

6
@mPrinC 这个问题说:“很遗憾,我无法在这台服务器上安装Cygwin或任何第三方工具,如UnxUtils。” - martin jakubik
8
因为这对我仍然很有用,所以我点了赞。而且它不需要“安装”,只需将其提取到某个地方即可。 - Rosdi Kasim

12
for /f %G in ('dir *.cpp *.h /s/b') do  ( find /i "what you search"  "%G") >> out_file.txt

1
这看起来很像这里的答案http://serverfault.com/a/506615,但我喜欢你将答案导入文件的方式。更容易消化。 - jinglesthula
1
仅供参考,使用findstr命令,您可以在命令行上获得突出显示的文件名,而在文本文件中则不会(显然)。因此,并不是说文本文件是更有用的格式。 - Martin Greenaway

10
< p > Select-String 对我来说效果最好。这里列出的所有其他选项,例如 findstr,在处理大文件时都无法正常工作。

这是一个例子:


select-string -pattern "<pattern>" -path "<path>"

注意:这需要使用PowerShell。


2
对我来说,必须在路径末尾添加“*.*”。例如:“C:\path\to\folder*.*”,否则会出现“访问被拒绝”的错误。 - Martin819

6

0

"findstr /spin /c:"string" [[drive:][path]filename[...]]"

与上面第二高的答案类似(由i_am_jorf于2009年3月30日22:26发布),它显示了以下示例:“findstr /spin /c:“string” [files]”

但是,运行“findstr /?”会显示没有定义为“[files]”的选项或参数。我相信他在这里暗示的是定义要搜索哪些文件的参数,而“findstr /?”将其描述为:“[[drive:][path]filename[...]]”。稍后使用以下方式定义它:

“[drive:][path]filename”-指定要搜索的一个或多个文件。

因此,为了不使用个人速记,如果要搜索某些文件,则按照findstr />定义的方式提供它:

“findstr /spin /c:“string” [[drive:][path]filename[...]]”


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接