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:
Serialization và Deserialization trong java
Generic Constructors in Java
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to Perform Partition of an Integer in All Possible Ways
Tránh lỗi NullPointerException trong Java như thế nào?
Default Password Encoder in Spring Security 5
Merging Two Maps with Java 8
Guide to Mustache with Spring Boot
The Modulo Operator in Java
Python Program to Convert Bytes to a String
Running Spring Boot Applications With Minikube
Python Program to Merge Mails
LinkedHashSet trong java
Thao tác với tập tin và thư mục trong Java
Difference Between Wait and Sleep in Java
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Program to Implement Queue using Linked List
Python Program to Count the Occurrence of an Item in a List
Handling Errors in Spring WebFlux
Spring Boot - Introduction
Introduction to Spring Security Expressions
Java Program to Check Multiplicability of Two Matrices
Spring Boot With H2 Database
Java Program to Represent Linear Equations in Matrix Form
Java – Byte Array to Reader
A Guide to TreeMap in Java
LinkedHashSet trong Java hoạt động như thế nào?
Deploy a Spring Boot WAR into a Tomcat Server
Shuffling Collections In Java
Java Program to Implement Interpolation Search Algorithm
A Quick JUnit vs TestNG Comparison
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS