1. 概述
本教程將教您如何在給定文件/文件中搜索模式,使用 Java 和第三方庫,例如 Unix4J 和 Grep4J。
2. 背景
Unix 擁有一個強大的命令,名為 grep – 意為 “全局正則表達式打印”。 它在給定文件集中搜索模式或正則表達式。
可以使用零個或多個選項與 grep 命令結合使用,以豐富搜索結果,我們將會在後續部分詳細探討這些選項。
如果您使用的是 Windows,可以按照帖子 這裏 安裝 bash。
3. 使用 Unix4j 庫
首先,讓我們看看如何使用 Unix4j 庫在文件中搜索模式。
在下面的示例中,我們將研究如何使用 Java 將 Unix grep 命令翻譯成 Java 代碼。
3.1. 構建配置
在您的 pom.xml 或 build.gradle 中添加以下依賴項:
<dependency>
<groupId>org.unix4j</groupId>
<artifactId>unix4j-command</artifactId>
<version>0.4</version>
</dependency>3.2. 使用 Grep 的示例
以下是 Unix 上的 Grep 示例:
grep "NINETEEN" dictionary.txt
在Java中,對應的代碼是:
@Test
public void whenGrepWithSimpleString_thenCorrect() {
int expectedLineCount = 4;
File file = new File("dictionary.txt");
List<Line> lines = Unix4j.grep("NINETEEN", file).toLineList();
assertEquals(expectedLineCount, lines.size());
}
另一個例子是在文件中使用反向文本搜索。以下是相同功能的 Unix 版本:
grep -v "NINETEEN" dictionary.txt
這是上述命令的Java版本:
@Test
public void whenInverseGrepWithSimpleString_thenCorrect() {
int expectedLineCount = 178687;
File file = new File("dictionary.txt");
List<Line> lines
= Unix4j.grep(Grep.Options.v, "NINETEEN", file). toLineList();
assertEquals(expectedLineCount, lines.size());
}
讓我們看看如何使用正則表達式在文件中搜索模式。以下是用於在整個文件中計算所有正則表達式模式的 Unix 版本:
grep -c ".*?NINE.*?" dictionary.txt
以下是上述命令的Java版本:
@Test
public void whenGrepWithRegex_thenCorrect() {
int expectedLineCount = 151;
File file = new File("dictionary.txt");
String patternCount = Unix4j.grep(Grep.Options.c, ".*?NINE.*?", file).
cut(CutOption.fields, ":", 1).toStringResult();
assertEquals(expectedLineCount, patternCount);
}4. 使用 Grep4J
接下來,讓我們看看如何使用 Grep4J 庫在本地或遠程位置的文件中搜索模式。
在下面的示例中,我們將研究如何使用 Java 將 Unix grep 命令轉換為 Java 命令。
4.1. 構建配置
在你的 pom.xml 或 build.gradle 中添加以下依賴項:
<dependency>
<groupId>com.googlecode.grep4j</groupId>
<artifactId>grep4j</artifactId>
<version>1.8.7</version>
</dependency><h3><strong>4.2. Grep 示例</strong></h3>
<p>Java 中的示例 grep,相當於:</p>
grep "NINETEEN" dictionary.txt
這是命令的Java版本:
@Test
public void givenLocalFile_whenGrepWithSimpleString_thenCorrect() {
int expectedLineCount = 4;
Profile localProfile = ProfileBuilder.newBuilder().
name("dictionary.txt").filePath(".").
onLocalhost().build();
GrepResults results
= Grep4j.grep(Grep4j.constantExpression("NINETEEN"), localProfile);
assertEquals(expectedLineCount, results.totalLines());
}
另一個例子是在文件中使用逆向文本搜索。以下是相同功能的 Unix 版本:
grep -v "NINETEEN" dictionary.txt
以下是 Java 版本:
@Test
public void givenRemoteFile_whenInverseGrepWithSimpleString_thenCorrect() {
int expectedLineCount = 178687;
Profile remoteProfile = ProfileBuilder.newBuilder().
name("dictionary.txt").filePath(".").
filePath("/tmp/dictionary.txt").
onRemotehost("172.168.192.1").
credentials("user", "pass").build();
GrepResults results = Grep4j.grep(
Grep4j.constantExpression("NINETEEN"), remoteProfile, Option.invertMatch());
assertEquals(expectedLineCount, results.totalLines());
}
讓我們看看如何使用正則表達式在文件中搜索模式。以下是用於在整個文件中計算所有正則表達式模式的 Unix 版本:
grep -c ".*?NINE.*?" dictionary.txt
以下是 Java 版本:
@Test
public void givenLocalFile_whenGrepWithRegex_thenCorrect() {
int expectedLineCount = 151;
Profile localProfile = ProfileBuilder.newBuilder().
name("dictionary.txt").filePath(".").
onLocalhost().build();
GrepResults results = Grep4j.grep(
Grep4j.regularExpression(".*?NINE.*?"), localProfile, Option.countMatches());
assertEquals(expectedLineCount, results.totalLines());
}5. 結論
在本快速教程中,我們演示瞭如何使用 Grep4j 和 Unix4J 在給定文件/文件中搜索模式。
此外,您還可以利用 JDK 中的正則表達式功能,輕鬆實現一些 grep 類似的基本功能。