什么是适用于 Windows 的好 grep 工具?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/87350/
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
What are good grep tools for Windows?
提问by Portman
采纳答案by Mark Biek
Based on recommendations in the comments, I've started using grepWinand it's fantastic and free.
根据评论中的建议,我已经开始使用grepWin,它非常棒且免费。
(I'm still a fan of PowerGREP, but I don't use it anymore.)
(我仍然是PowerGREP的粉丝,但我不再使用它了。)
I know you already mentioned it, but PowerGREP is awesome.
我知道您已经提到过,但是 PowerGREP很棒。
Some of my favorite features are:
我最喜欢的一些功能是:
- Right-click on a folder to run PowerGREP on it
- Use regular expressions or literal text
- Specify wildcards for files to include & exclude
- Search & replace
- Preview mode is nice because you can make sure you're replacing what you intend to.
- 右键单击文件夹以在其上运行 PowerGREP
- 使用正则表达式或文字文本
- 为要包含和排除的文件指定通配符
- 搜索和替换
- 预览模式很好,因为您可以确保您正在替换您打算替换的内容。
Now I realize that the other grep tools can do all of the above. It's just that PowerGREP packages all of the functionality into a very easy-to-use GUI.
现在我意识到其他 grep 工具可以完成上述所有操作。只是 PowerGREP 将所有功能打包到一个非常易于使用的 GUI 中。
From the same wonderful folks who brought you RegexBuddyand who I have no affiliation with beyond loving their stuff.(It should be noted that RegexBuddy includes a basic version of grep (for Windows) itself and it costs a lot less than PowerGREP.)
来自给你带来RegexBuddy的同样优秀的人,除了喜欢他们的东西之外,我与他们没有任何关系。(应该注意的是,RegexBuddy 本身包含一个基本版本的 grep(适用于 Windows),并且它的成本比 PowerGREP 低很多。)
Additional solutions
其他解决方案
Existing Windows commands
现有的 Windows 命令
- FINDSTR
- Select-Stringin PowerShell
Linux command implementations on Windows
Windows 上的 Linux 命令实现
Grep tools with a graphical interface
具有图形界面的 Grep 工具
Additional Grep tools
其他 Grep 工具
回答by Ray Hayes
FINDSTRis fairly powerful, supports regular expressionsand has the advantages of being on all Windows machines already.
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
Example usage: findstr text_to_find *
or to search recursively findstr /s text_to_find *
示例用法:findstr text_to_find *
或递归搜索findstr /s text_to_find *
回答by Stan Sarama
GrepWinFree and open source (GPL)
GrepWin免费和开源 (GPL)
I've been using grepWin which was written by one of the tortoisesvn guys. Does the job on Windows...
我一直在使用 grepWin,它是由 Tortoisesvn 的一个人编写的。在 Windows 上工作吗...
回答by VonC
Update July 2013:
2013 年 7 月更新:
Another grep tool I now use all the time on Windows is AstroGrep:
我现在一直在 Windows 上使用的另一个 grep 工具是AstroGrep:
Its ability to show me more than just the line search (i.e. the --context=NUM of a command-line grep) is invaluable.
And it is fast. Very fast, even on an old computer with non-SSDdrive (I know, they used to do this hard drive with spinningdisks, called platters, crazy right?)
它向我展示的不仅仅是行搜索(即命令行 grep 的 --context=NUM)的能力是无价的。
而且速度很快。非常快,即使在带有非SSD驱动器的旧计算机上也是如此(我知道,他们过去常常使用旋转磁盘来制作这种硬盘驱动器,称为拼盘,很疯狂吧?)
It is free.
It is portable (simple zip archive to unzip).
这是免费的。
它是便携的(简单的 zip 压缩包解压)。
Original answer October 2008
原始答案 2008 年 10 月
Gnu Grepis alright
grep的牛羚是好的
You can download it for example here: (site ftp)
例如,您可以在此处下载:(站点 ftp)
All the usual optionsare here.
所有常用选项都在这里。
That, combined with gawkand xargs(includes 'find', from GnuWin32), and you can really script like you were on Unix!
结合gawk和xargs(包括'find',来自GnuWin32),您就可以像在 Unix 上一样编写脚本!
See also the options I am using to grep recursively:
另请参阅我用于递归 grep的选项:
grep --include "*.xxx" -nRHI "my Text to grep" *
回答by Jay Bazuzi
PowerShell's Select-String cmdlet was fine in v1.0, but is significantly better for v2.0. Having PowerShell built in to recent versions of Windows means your skills here will always useful, without first installing something.
PowerShell 的 Select-String cmdlet 在 v1.0 中很好,但在 v2.0 中明显更好。将 PowerShell 内置到最新版本的 Windows 中意味着您在这里的技能将始终有用,而无需先安装某些东西。
New parameters added to Select-String: Select-String cmdlet now supports new parameters, such as:
- -Context: This allows you to see lines before and after the match line
- -AllMatches: which allows you to see all matches in a line (Previously, you could see only the first match in a line)
- -NotMatch: Equivalent to grep -v o
- -Encoding: to specify the character encoding
添加到 Select-String 的新参数:Select-String cmdlet 现在支持新参数,例如:
- -Context:这允许您查看匹配行之前和之后的行
- -AllMatches:允许您查看一行中的所有匹配项(以前,您只能看到一行中的第一个匹配项)
- -NotMatch:相当于grep -vo
- -Encoding:指定字符编码
I find it expedient to create an function gcir
for Get-ChildItem -Recurse .
, with smarts to pass parameters correctly, and an alias ss
for Select-String
. So you an write:
我觉得适宜创建一个功能gcir
为Get-ChildItem -Recurse .
,以智慧来正确地传递参数,以及别名ss
为Select-String
。所以你写:
gcir *.txt | ss foo
gcir *.txt | ss foo
回答by jasonmray
It may not exactly fall into the 'grep' category, but I couldn't get by on Windows without a utility called AgentRansack. It's a gui-based "find in files" utility with regex support. It's dead simple to right-click on a folder, hit "ransack.." and find files containing what you're looking for. Extremely fast too.
它可能不完全属于“grep”类别,但如果没有名为AgentRansack的实用程序,我就无法在 Windows 上使用。这是一个基于 gui 的“在文件中查找”实用程序,具有正则表达式支持。右键单击一个文件夹,点击“ransack..”并找到包含您要查找的内容的文件非常简单。也特别快。
回答by jj.
It's been a couple of years since you asked the question, but I'd recommend AstroGrep (http://astrogrep.sourceforge.net).
你问这个问题已经有几年了,但我推荐 AstroGrep ( http://astrogrep.sourceforge.net)。
It's free, open source, and has a simple interface. I use it to search code all the time.
它是免费的、开源的,并且有一个简单的界面。我一直用它来搜索代码。
回答by jslatts
PowerShell has been mentioned a few times. Here is how you would actually use it in a grepish way:
PowerShell 已被多次提及。以下是您将如何以一种令人讨厌的方式实际使用它:
Get-ChildItem -recurse -include *.txt | Select-String -CaseSensitive "SomeString"
It recursively searches all text files in the current directory tree for SomeString
with case sensitivity.
它SomeString
以区分大小写的方式递归搜索当前目录树中的所有文本文件。
Even better, run this:
更好的是,运行这个:
function pgrep { param([string]$search, [string]$inc) Get-ChildItem -recurse -include $inc | Select-String -CaseSensitive $search }
Then do:
然后做:
pgrep SomeStringToSearch *.txt
Then to really make it magical, add the function alias to your PowerShell Profileand you can almost dull the pain of not having proper command line tools.
然后,真正让它变得神奇,将函数别名添加到您的PowerShell 配置文件中,您几乎可以消除没有适当命令行工具的痛苦。
回答by Daniel Magnusson
Git on Windows = grep in cmd.exe
Windows 上的 Git = cmd.exe 中的 grep
I just found out installing Gitwill give you some basic Linux commands: cat, grep, scp and all other good ones.
我刚刚发现安装Git会给你一些基本的 Linux 命令:cat、grep、scp 和所有其他好的命令。
Install then add the Git bin folder to your PATH and then your cmd.exe has basic Linux functionality!
安装然后将 Git bin 文件夹添加到您的 PATH 中,然后您的 cmd.exe 具有基本的 Linux 功能!