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 MVC + Thymeleaf 3.0: New Features
Phân biệt JVM, JRE, JDK
Jackson – JsonMappingException (No serializer found for class)
Comparing Two HashMaps in Java
Java Program to Find Maximum Element in an Array using Binary Search
Java Program to Perform Complex Number Multiplication
A Guide to ConcurrentMap
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
@DynamicUpdate with Spring Data JPA
Comparing Dates in Java
Removing Elements from Java Collections
Convert String to Byte Array and Reverse in Java
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Java Program to Implement Graph Structured Stack
Java Program to Find the GCD and LCM of two Numbers
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Java Program to Construct an Expression Tree for an Postfix Expression
Spring Boot Security Auto-Configuration
Java Program to Generate a Sequence of N Characters for a Given Specific Case
A Quick Guide to Spring Cloud Consul
Java TreeMap vs HashMap
Working With Maps Using Streams
Java Program to Implement Circular Singly Linked List
Guide to the Volatile Keyword in Java
Lớp Collections trong Java (Collections Utility Class)
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Python Program to Swap Two Variables
ClassNotFoundException vs NoClassDefFoundError
Java Program to Implement Repeated Squaring Algorithm
Converting Between an Array and a Set in Java
Java Program to Implement ArrayList API
Java Program to implement Dynamic Array