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 Boot - Database Handling
Spring MVC and the @ModelAttribute Annotation
Guide to DelayQueue
Java Program to Perform Complex Number Multiplication
Java Program to Implement Brent Cycle Algorithm
Registration – Password Strength and Rules
Python Program to Remove Punctuations From a String
XML Serialization and Deserialization with Jackson
Spring Cloud AWS – EC2
Tiêu chuẩn coding trong Java (Coding Standards)
Guava Collections Cookbook
Java Byte Array to InputStream
Java Program to Implement Segment Tree
HashSet trong java
Java Program to Describe the Representation of Graph using Incidence Matrix
Introduction to Apache Commons Text
Java Program to Implement Binomial Heap
Reactive WebSockets with Spring 5
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Một số ký tự đặc biệt trong Java
Spring Boot - Scheduling
Java Program to Find Transitive Closure of a Graph
A Custom Data Binder in Spring MVC
Java Program to Implement EnumMap API
Apache Commons Collections BidiMap
Java Program to Implement Insertion Sort
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Spring Boot - Interceptor
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Từ khóa static và final trong java
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Introduction to PCollections