Java Program to Implement the Program Used in grep/egrep/fgrep

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:

Collect a Java Stream to an Immutable Collection
Guide to Dynamic Tests in Junit 5
Setting the Java Version in Maven
Derived Query Methods in Spring Data JPA Repositories
The Spring @Controller and @RestController Annotations
Java Program to Implement Binary Heap
A Guide to Spring Boot Admin
Java Program to Find the Longest Path in a DAG
Java Program to implement Dynamic Array
Java Program to Implement Singly Linked List
Java Program to Perform Stooge Sort
Hướng dẫn Java Design Pattern – DAO
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Constructor Dependency Injection in Spring
Lớp Collections trong Java (Collections Utility Class)
Java String Conversions
Java Program to Implement Sieve Of Sundaram
Java Program to Generate a Random Subset by Coin Flipping
Create Java Applet to Simulate Any Sorting Technique
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Java Program to Check the Connectivity of Graph Using BFS
Guide To CompletableFuture
Java Program to Implement Threaded Binary Tree
Java Program to Implement Tarjan Algorithm
Python Program to Convert Two Lists Into a Dictionary
Java Program to Implement Stack using Linked List
Introduction to Netflix Archaius with Spring Cloud
Java Web Services – JAX-WS – SOAP
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Get Line Count of a File
Java Program to Implement Naor-Reingold Pseudo Random Function
Java Program to Perform the Shaker Sort