Windows 递归 grep 命令行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/698038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows recursive grep command-line
提问by Andy White
I need to do a recursive grep in Windows, something like this in Unix/Linux:
我需要在 Windows 中执行递归 grep,在 Unix/Linux 中是这样的:
grep -i 'string' `find . -print`
or the more-preferred method:
或更优选的方法:
find . -print | xargs grep -i 'string'
I'm stuck with just cmd.exe, so I only have Windows built-in commands. I can't install Cygwin, or any 3rd party tools like UnxUtilson this server unfortunately. I'm not even sure I can install PowerShell. Any suggestions using only cmd.exe built-ins (Windows 2003 Server)?
我只使用 cmd.exe,所以我只有 Windows 内置命令。不幸的是,我无法在此服务器上安装Cygwin或任何第三方工具,例如UnxUtils。我什至不确定我是否可以安装 PowerShell。任何仅使用 cmd.exe 内置程序(Windows 2003 Server)的建议?
回答by Michael Burr
findstr
can do recursive searches (/S) and supports some variant of regex syntax (/R).
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.
回答by i_am_jorf
findstr /spin /c:"string" [files]
The parameters have the following meanings:
参数含义如下:
s
= recursivep
= skip non-printable charactersi
= case insensitiven
= print line numbers
s
= 递归p
= 跳过不可打印的字符i
= 不区分大小写n
= 打印行号
And the string to search for is the bit you put in quotes after /c:
并且要搜索的字符串是您在后面加引号的位 /c:
回答by khichar.anil
I just searched a text with following command which listed me all the file names containing my specified 'search text'.
我只是使用以下命令搜索了一个文本,其中列出了包含我指定的“搜索文本”的所有文件名。
C:\Users\ak47\Desktop\trunk>findstr /S /I /M /C:"search text" *.*
回答by mPrinC
I recommend a really great tool:
我推荐一个非常棒的工具:
native unix utils:
本机 unix 实用程序:
Just unpack them and put that folder into your PATH environment variable and voila! :)
只需解压缩它们并将该文件夹放入您的 PATH 环境变量中,瞧!:)
Works like a charm, and there are much more then just grep ;)
像魅力一样工作,还有更多只是 grep ;)
回答by Tonatio
Recursive search for import
word inside src
folder:
递归搜索文件夹import
内的单词src
:
> findstr /s import .\src\*
回答by goldenBoy
for /f %G in ('dir *.cpp *.h /s/b') do ( find /i "what you search" "%G") >> out_file.txt
回答by blizz
Select-String
worked best for me. All the other options listed here, such as findstr
, didn't work with large files.
Select-String
对我来说效果最好。此处列出的所有其他选项(例如 )findstr
均不适用于大文件。
Here's an example:
下面是一个例子:
select-string -pattern "<pattern>" -path "<path>"
note: This requires Powershell
注意:这需要Powershell
回答by Andy Lester
If you have Perl installed, you could use ack, available at http://beyondgrep.com/.
如果您安装了 Perl,则可以使用ack,可从http://beyondgrep.com/ 获得。