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:
Shuffling Collections In Java
Sử dụng CyclicBarrier trong Java
Deque và ArrayDeque trong Java
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Constructor Injection in Spring with Lombok
Spring Boot - Quick Start
Programmatic Transaction Management in Spring
Java Program to Perform Naive String Matching
JUnit 5 for Kotlin Developers
Python Program to Create a Long Multiline String
An Intro to Spring Cloud Security
HttpClient 4 – Send Custom Cookie
The “final” Keyword in Java
Spring 5 WebClient
Java Program to Implement the RSA Algorithm
How to Find an Element in a List with Java
Runnable vs. Callable in Java
Java Program to Implement SynchronosQueue API
Spring 5 Testing with @EnabledIf Annotation
Python Program to Check if a Number is Positive, Negative or 0
How to Get a Name of a Method Being Executed?
New in Spring Security OAuth2 – Verify Claims
Jackson Exceptions – Problems and Solutions
Tips for dealing with HTTP-related problems
Handling Errors in Spring WebFlux
Java Program to subtract two large numbers using Linked Lists
An Intro to Spring Cloud Contract
Java Program to Implement Bubble Sort
Dockerizing a Spring Boot Application
Query Entities by Dates and Times with Spring Data JPA
Service Registration with Eureka
Java Program to Implement Johnson’s Algorithm