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:
Java Program to Implement TreeSet API
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Jackson Exceptions – Problems and Solutions
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Jackson Annotation Examples
Spring Boot: Customize the Jackson ObjectMapper
Hướng dẫn sử dụng Java Generics
Introduction to Spring Cloud CLI
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Finding the Differences Between Two Lists in Java
Server-Sent Events in Spring
How to Get All Dates Between Two Dates?
Java Program to Generate Random Numbers Using Middle Square Method
Java Program to Find the Vertex Connectivity of a Graph
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
An Intro to Spring Cloud Security
Java Program to Implement JobStateReasons API
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Tránh lỗi NullPointerException trong Java như thế nào?
Spring Cloud Series – The Gateway Pattern
Java Program to Implement Rope
The StackOverflowError in Java
Spring @Primary Annotation
Java Program to Find Transitive Closure of a Graph
Weak References in Java
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Python Program to Find LCM
Java Program to Convert a Decimal Number to Binary Number using Stacks
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Guide to the Volatile Keyword in Java