This is a java program to implement grep linux command.
Here is the source code of the Java Program to Implement the Program Used in grep/egrep/fgrep. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
package com.maixuanviet.setandstring;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GrepCommandImplementation
{
public static void main(String[] argv) throws Exception
{
Scanner sc = new Scanner(System.in);
System.out
.println("Enter the string to match from GrepCommandImplementation.java file: ");
Pattern pattern = Pattern.compile(sc.next());
Matcher matcher = pattern.matcher("");
String file = "src/com/sanfoundry/setandstring/GrepCommandImplementation.java";
BufferedReader br = null;
String line;
try
{
br = new BufferedReader(new FileReader(file));
}
catch (IOException e)
{
System.err.println("Cannot read '" + file + "': " + e.getMessage());
}
while ((line = br.readLine()) != null)
{
matcher.reset(line);
if (matcher.find())
{
System.out.println(file + ": " + line);
}
}
br.close();
sc.close();
}
}
Output:
$ javac GrepCommandImplementation.java
$ java GrepCommandImplementation
Enter the string to match from GrepCommandImplementation.java file:
println
src/com/sanfoundry/setandstring/GrepCommandImplementation.java: .println("Enter the string to match from GrepCommandImplementation.java file: ");
src/com/sanfoundry/setandstring/GrepCommandImplementation.java: System.err.println("Cannot read '" + file + "': " + e.getMessage());
src/com/sanfoundry/setandstring/GrepCommandImplementation.java: System.out.println(file + ": " + line);
Related posts:
Spring Cloud Connectors and Heroku
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
New Features in Java 10
A Guide to the Java LinkedList
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Java 8 StringJoiner
Batch Processing with Spring Cloud Data Flow
Spring – Injecting Collections
So sánh ArrayList và LinkedList trong Java
Java Program to Perform Stooge Sort
Java Program to Implement Fermat Primality Test Algorithm
Java 8 Predicate Chain
Spring Boot - Service Components
Circular Dependencies in Spring
Java Program to Implement Sieve Of Eratosthenes
Java – Generate Random String
Write/Read cookies using HTTP and Read a file from the internet
Hướng dẫn sử dụng Lớp FilePermission trong java
Java Program to Implement Maximum Length Chain of Pairs
Java Program to Generate Random Numbers Using Middle Square Method
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Ways to Iterate Over a List in Java
A Comparison Between Spring and Spring Boot
HashSet trong java
Database Migrations with Flyway
Filtering and Transforming Collections in Guava
Spring Boot - Database Handling
Python Program to Check the File Size
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Program to Find the Longest Path in a DAG
Guide to PriorityBlockingQueue in Java