Windows命令提示符中是否有与grep类似的实用程序,或者是否有第三方工具?
当前回答
我已经有一段时间没有使用它们了,但是Borland(现在是Embarcadero)在他们的C/ c++编译器中包含了命令行grep。一段时间以来,他们在注册后就可以免费下载5.5版本。
其他回答
我知道这是一个有点老的话题,但是,还有一件事你可以做。我在一个开发人员虚拟机上工作,没有互联网接入,空闲磁盘空间非常有限,所以我使用了安装在上面的java。
编译一个小java程序,打印正则表达式匹配到控制台。将jar放在你系统的某个地方,创建一个批处理来执行它,并将文件夹添加到你的PATH变量中:
JGrep.java:
package com.jgrep;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JGrep {
public static void main(String[] args) throws FileNotFoundException, IOException {
int printGroup = -1;
if (args.length < 2) {
System.out.println("Invalid arguments. Usage:");
System.out.println("jgrep [...-MODIFIERS] [PATTERN] [FILENAME]");
System.out.println("Available modifiers:");
System.out.println(" -printGroup - will print the given group only instead of the whole match. Eg: -printGroup=1");
System.out.println("Current arguments:");
for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "]=" + args[i]);
}
return;
}
Pattern pattern = null;
String filename = args[args.length - 1];
String patternArg = args[args.length - 2];
pattern = Pattern.compile(patternArg);
int argCount = 2;
while (args.length - argCount - 1 >= 0) {
String arg = args[args.length - argCount - 1];
argCount++;
if (arg.startsWith("-printGroup=")) {
printGroup = Integer.parseInt(arg.substring("-printGroup=".length()));
}
}
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
}
Matcher matcher = pattern.matcher(sb.toString());
int matchesCount = 0;
while (matcher.find()) {
if (printGroup > 0) {
System.out.println(matcher.group(printGroup));
} else {
System.out.println(matcher.group());
}
matchesCount++;
}
System.out.println("----------------------------------------");
System.out.println("File: " + filename);
System.out.println("Pattern: " + pattern.pattern());
System.out.println("PrintGroup: " + printGroup);
System.out.println("Matches: " + matchesCount);
}
}
C:\jgrep\jgrep.bat(连同jgrep.jar):
@echo off
java -cp c:\jgrep\jgrep.jar com.jgrep.JGrep %*
并在PATH环境变量的末尾添加c:\jgrep。
现在只需从任何地方调用jgrep "expression" file.txt即可。
我需要从我的表达式中打印一些特定的组,所以我添加了一个修饰符,并将其称为jgrep -printGroup=1 "expression" file.txt。
我知道这是一个老问题,但我看到这个帖子寻求答案。我找到了一个,所以把它添加到这里,作为互联网的集体记忆
Powershell:选择string 模块: Microsoft.PowerShell.Utility
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string
以及一篇包含高级示例的内容丰富的博客文章:“如何在PowerShell中“grep”” https://antjanus.com/blog/web-development-tutorials/how-to-grep-in-powershell/
举个简单的例子: 猫包。选择字符串-模式webpack . json | ls ./src/components/ | Select-String -Pattern View
C:>猫帖。md | Select-String -Pattern "^\w*:"
Yes there is only one program for Windows PC which have solid GUI and it is essential util for me. I work as a developer and on every computer I've had, first thing install XFind program. It is created in 1997 and till now version is 1.0 and till now works and it is the best. Frequently I need to search some string in a ".cs", ".aspx", ".sct" (Visual FoxPro form code file) or just ".*" and XFind scans all files and show me files and another great thing is that you can look where string is in the file. XFind has also some kind of editor. If it binary file it will show you string finded. Try it and use it forever if you are developer like me.
如果你必须使用纯Windows,那么除了上面提到的Powershell选项,你还可以使用VBScript,它有很好的RegEx支持。
MS在Technet上还有一个不错的脚本编写区,为管理员提供了大量的示例。
使用Cygwin……
它有32位和64位版本 它在Windows 2000中工作良好(*) Windows 10或Server 2019
我用Cygwin很久了…… 最近尝试用windows - linux -子系统来替代…
不会太久…… 我很快又回到了Cygwin……
更加灵活、可控和丰富…… 也不那么打扰…
只需将\bin添加到路径… 你可以在Windows/Batch/Powershell中使用它… 或者在DOS-Box里…或者装在powershell盒子里…
你也可以安装很多很棒的软件包 这真的有用……比如nginX或PHP… 我甚至在我的IIS中使用Cygwin PHP包…
作为奖励,我们也可以在bash shell中使用它… (我认为这是最初的意图;-))
推荐文章
- 我如何找到哪个程序正在使用端口80在Windows?
- 在Windows中有像GREP这样的模式匹配实用程序吗?
- 如何在Windows命令提示符下运行.sh ?
- 如何从命令行将每两行合并为一行?
- 如何从命令行在windows中找到mysql数据目录
- 匹配前后的Grep字符?
- (grep)正则表达式匹配非ascii字符?
- 在没有事件源注册的情况下写入Windows应用程序事件日志
- 如何从另一个文件A中删除文件B中出现的行?
- 无法在Windows上从/usr/local/ssl/openssl.cnf加载配置信息
- GIT克隆在windows中跨本地文件系统回购
- 如何运行一个PowerShell脚本而不显示窗口?
- PowerShell:仅为单个命令设置环境变量
- 为什么这个Windows批处理文件只执行第一行,而在命令shell中执行所有三行?
- 环境变量存储在Windows注册表的哪里?