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 Queue using Two Stacks
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Handling Errors in Spring WebFlux
How to Delay Code Execution in Java
Java Program to Find kth Largest Element in a Sequence
Comparing Objects in Java
Vector trong Java
Python Program to Illustrate Different Set Operations
Hướng dẫn Java Design Pattern – State
Java – Reader to String
Hướng dẫn Java Design Pattern – Intercepting Filter
Java Program to Generate Random Numbers Using Multiply with Carry Method
Spring MVC Content Negotiation
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Check if a Matrix is Invertible
Model, ModelMap, and ModelAndView in Spring MVC
LinkedHashSet trong java
Java – Write to File
Java Program to Implement IdentityHashMap API
Number Formatting in Java
Converting a Stack Trace to a String in Java
Send email with SMTPS (eg. Google GMail)
Date Time trong Java 8
Spring Security Custom AuthenticationFailureHandler
Optional trong Java 8
Java Optional as Return Type
Shuffling Collections In Java
Iterating over Enum Values in Java
Java Program to subtract two large numbers using Linked Lists
HttpClient 4 Cookbook
Spring WebClient Requests with Parameters
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm