FINDSTR 功能强大,支持正则表达式,并且已经预装在所有 Windows 机器上,具有优越性。
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 occurances 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
示例用法:findstr text_to_find *
或递归搜索 findstr /s text_to_find *
(我仍然喜欢PowerGREP,但我不再使用它了。)
我知道你已经提到过它,但是PowerGREP真的很棒。
我最喜欢的功能之一是:
现在我意识到其他grep工具也可以完成以上所有任务。只是PowerGREP将所有功能打包成一个易于使用的GUI。
来自同样精彩的人们,他们推出了RegexBuddy,我并没有任何关联,只是非常喜欢他们的东西。(应该注意的是,RegexBuddy本身包括一个Windows版本的基本grep,价格远低于PowerGREP。)
更新于2013年7月:
另一个我现在在Windows上经常使用的grep工具是AstroGrep:
原始答案为2008年10月
Gnu Grep还可以
你可以在这里下载它:(site ftp)
所有常见选项都在这里。
结合使用gawk和xargs(包括'find',来自GnuWin32),你可以像在Unix上一样编写脚本!
另请参阅我用于递归grep的选项:
grep --include "*.xxx" -nRHI "my Text to grep" *
PowerShell的Select-String命令在v1.0中就很好了,但在v2.0中它显著改进。将PowerShell内置到最近版本的Windows中意味着您在这里的技能将始终有用,无需先安装任何东西。
Select-String添加了新参数:Select-String命令现在支持新参数,例如:
- -Context:这允许您查看匹配行之前和之后的行
- -AllMatches:允许您在一行中查看所有匹配项(以前,您只能查看一行中的第一个匹配项)
- -NotMatch:等同于grep -v o
- -Encoding:指定字符编码
我发现创建一个函数gcir
来执行Get-ChildItem -Recurse .
非常方便,可以聪明地正确传递参数,并为Select-String
创建一个别名ss
。所以你可以写:
gcir *.txt | ss foo
ls . *.txt -r| sls -ca "SomeString"
。 - Rynantsls
代表Select-String
真的太长了吗? - Joey这可能并不完全属于“grep”类别,但在Windows上我无法没有一个叫做AgentRansack的实用程序。它是一个基于GUI的“查找文件”实用程序,支持正则表达式。
只需右键单击文件夹,点击“ransack..”即可查找包含所需内容的文件。它非常快速。
PowerShell已经被提到了几次。以下是如何实际使用它进行grepish操作的方法:
Get-ChildItem -recurse -include *.txt | Select-String -CaseSensitive "SomeString"
它会以区分大小写的方式递归搜索当前目录树中所有文本文件,寻找SomeString
。
更好的方法是运行以下命令:
function pgrep { param([string]$search, [string]$inc) Get-ChildItem -recurse -include $inc | Select-String -CaseSensitive $search }
然后执行:
pgrep SomeStringToSearch *.txt
如果想要更加便利,可以在PowerShell配置文件中添加函数别名,这样就可以减轻没有合适命令行工具的痛苦了。
我建议使用AstroGrep。
它是免费的、开源的,并且有一个简单的界面。我经常使用它来搜索代码。
在 Windows 上使用 Git = 在 cmd.exe 中使用 grep
我刚发现安装Git会提供一些基本的 Linux 命令:cat、grep、scp以及其他好用的命令。
安装后,将 Git bin 文件夹添加到 PATH 中,然后您的 cmd.exe 就具备了基本的 Linux 功能!