Windows递归grep命令行

k5hmc34c  于 2023-06-24  发布在  Windows
关注(0)|答案(9)|浏览(179)

我需要在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)?

l2osamch

l2osamch1#

findstr可以执行递归搜索(/S),并支持regex语法的某些变体(/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.
7fyelxc5

7fyelxc52#

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

参数具有以下含义:

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

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

xtupzzrd

xtupzzrd3#

我只是用下面的命令搜索了一个文本,其中列出了包含我指定的“搜索文本”的所有文件名。

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

tjvv9vkg4#

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

findstr /s import .\src\*
5lhxktic

5lhxktic5#

我推荐一个很棒的工具:
native unix utils:

只要将它们解包并将该文件夹放入PATH环境变量中,瞧!:)
工作起来就像一个魅力,还有更多,然后只是grep;)

isr3a4wc

isr3a4wc6#

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

oxf4rvwz7#

Select-String最适合我这里列出的所有其他选项,如findstr,都不适用于大文件。
下面是一个例子:

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

注意:需要PowerShell

nbewdwxp

nbewdwxp8#

如果你安装了Perl,你可以使用ack,可以在http://beyondgrep.com/上找到。

vtwuwzda

vtwuwzda9#

“findstr /spin /c:“string”[[drive:][path]filename[...]]”
类似于上面第二高的答案(由i_am_约尔夫于2009年3月30日22:26),显示了以下示例:“findstr /spin /c:“string”[文件]”
但是,运行“findstr /?“显示没有定义为“[文件]"的选项或参数。我相信他在这里暗示的是定义要搜索哪些文件的参数“findstr /?”描述为:“[[drive:][path]filename[...]]”它后来用以下方法定义了这个:“[drive:][path]filename”-指定要搜索的文件。
因此,为了不使用个人速记,我提供了findstr />在搜索某些文件时定义它的方式:“findstr /spin /c:“string”[[drive:][path]filename[...]]”

相关问题