【问题标题】:Windows recursive grep command-lineWindows 递归 grep 命令行
【发布时间】:2010-10-16 10:20:02
【问题描述】:

我需要在 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)有什么建议吗?

【问题讨论】:

  • 没有powershell好难,为什么不能安装?
  • 系统管理员正在锁定我们服务器上的权限。如果有人有任何关于 powershell 的建议,请扔掉,我看看我们是否可以安装 PowerShell。
  • 顺便说一句,我发现在linux中最好写:“find .|xargs grep -i string”。不同之处在于,如果 find 返回一个很长的列表,您可能会超过最大命令长度(这发生在我身上),并且您根本无法 grep。对于每个找到的文件,使用 xargs grep 调用一次。
  • Grep 的许多版本,包括 Gnu Grep,都提供内置递归搜索 (gnu.org/software/grep/manual/…),因此您的搜索可以写成 grep -i 'string' -R .,就像 @NathanFellman 建议的那样,避免了过长的命令。

标签: windows command-line grep


【解决方案1】:

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.

【讨论】:

  • findstr 是 grep 的绝佳替代品。我倾向于使用 findstr /sinp(递归,不区分大小写,跳过二进制文件,并显示行号)
  • 不幸的是,根据文档和我尝试使用的模式,findstr 对正则表达式的支持非常有限。
  • 叹息,相信微软会添加一个新的实用程序 (findstr) 而不是修复现有的实用程序 (find)。如果你希望 findstr 可以计算行数,那么使用这个 -- findstr [options] | find /c /v "" -- 使用 findstr 匹配行并 find 对它们进行计数。是的,find 认为没有行匹配一个空字符串,所以 /v 每一行都会匹配。
  • 他们不想破坏依赖 find 破坏行为的现有管道。
【解决方案2】:
findstr /spin /c:"string" [files]

参数含义如下:

  • s = 递归
  • p = 跳过不可打印的字符
  • i = 不区分大小写
  • n = 打印行号

要搜索的字符串是您在/c: 后面加上引号的位

【讨论】:

  • 对不起。你能添加一个例子吗? spin 是什么?是要查找的文本行吗?不是 /g 或 /f 用于指定文件吗?那么方括号是怎么回事?
  • findstr /? 解释了每个参数。 s = 递归,p = 跳过不可打印字符,i = 不区分大小写,n = 打印行号。你不一定需要所有这些,但我喜欢它们,而且spin 很容易记住。要搜索的字符串是您在/c: 之后加上引号的位。
  • 哦哈哈。我做了一个/?,但实际上我不知道修饰符被用作/spin。我以为它们被用作/s/p/i/n
  • 是的,一般来说。一些 cmd 程序让你在/s 上放松一下。这是一。并非所有人都让你这样做。要知道,cmd很特别。
  • 它作为 Windows 的一部分分发,因此满足原始海报的要求,可以使用 cmd.exe 完成,不需要安装任何额外的软件。
【解决方案3】:

如果您安装了 Perl,则可以使用 ack,地址为 http://beyondgrep.com/

【讨论】:

    【解决方案4】:

    我推荐一个非常棒的工具:

    原生 unix 工具:

    只需解压缩它们并将该文件夹放入您的 PATH 环境变量中,瞧! :)

    就像一个魅力,还有更多不仅仅是 grep ;)

    【讨论】:

    • @mPrinC 问题是“不幸的是,我无法在此服务器上安装 Cygwin 或 UnxUtils 等任何 3rd 方工具”。
    • 赞成,因为它对我仍然有用。它也不需要“安装”,只需将其解压到某个地方即可。
    【解决方案5】:
    for /f %G in ('dir *.cpp *.h /s/b') do  ( find /i "what you search"  "%G") >> out_file.txt
    

    【讨论】:

    • 这看起来与serverfault.com/a/506615 此处的答案相似,但我喜欢您将答案传送到文件的事实。更容易食用。
    • 仅供参考,使用 findstr,您会在命令行上获得突出显示的文件名,而您不会在文本文件中获得它(显然)。因此,文本文件并不是一种更有用的格式。
    【解决方案6】:

    我刚刚使用以下命令搜索了一个文本,该命令列出了包含我指定的“搜索文本”的所有文件名。

    C:\Users\ak47\Desktop\trunk>findstr /S /I /M /C:"search text" *.*
    

    【讨论】:

      【解决方案7】:

      Select-String 最适合我。此处列出的所有其他选项(例如 findstr)均不适用于大文件。

      这是一个例子:

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

      注意:这需要 Powershell

      【讨论】:

      • 对我来说,有必要将"*.*" 添加到路径的末尾。示例:"C:\path\to\folder\*.*" 否则我会收到“拒绝访问”错误。
      【解决方案8】:

      src 文件夹内递归搜索import 单词:

      > findstr /s import .\src\*
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多