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:
Creating a Web Application with Spring 5
JUnit5 @RunWith
Python Program to Find Numbers Divisible by Another Number
A Guide to System.exit()
Python Program to Find Factorial of Number Using Recursion
Guide to Character Encoding
Java Program to Implement Skew Heap
Đồng bộ hóa các luồng trong Java
Python Program to Trim Whitespace From a String
New Features in Java 12
Python Program to Delete an Element From a Dictionary
Java 8 Streams peek() API
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Java – Reader to InputStream
Python Program to Display Calendar
Guide to the Fork/Join Framework in Java
Hướng dẫn Java Design Pattern – Decorator
Quick Guide on Loading Initial Data with Spring Boot
Java Program to Implement K Way Merge Algorithm
Jackson – JsonMappingException (No serializer found for class)
Spring Boot - Flyway Database
Python Program to Find Armstrong Number in an Interval
Tính đa hình (Polymorphism) trong Java
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Implementing a Runnable vs Extending a Thread
Java Program to Implement Meldable Heap
Java Program to Find Minimum Element in an Array using Linear Search
Introduction to Using FreeMarker in Spring MVC
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Java Program to Implement String Matching Using Vectors
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Ép kiểu trong Java (Type casting)