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:

Java Program to Implement Knapsack Algorithm
Python Program to Find the Largest Among Three Numbers
Java Program to Print only Odd Numbered Levels of a Tree
Python Program to Print Output Without a Newline
Guide to Escaping Characters in Java RegExps
Getting the Size of an Iterable in Java
Java Program to Check for balanced parenthesis by using Stacks
Java Program to Implement Rope
Spring Boot Application as a Service
Java 8 – Powerful Comparison with Lambdas
Java Program to Implement Direct Addressing Tables
Checking for Empty or Blank Strings in Java
Java Program to Find Inverse of a Matrix
Guide to Java 8 groupingBy Collector
A Guide to WatchService in Java NIO2
A Quick Guide to Using Keycloak with Spring Boot
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Đồng bộ hóa các luồng trong Java
Python Program to Find Hash of File
Python Program to Capitalize the First Character of a String
Hashtable trong java
Spring Boot - Logging
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Java Program to Implement Fibonacci Heap
The Dining Philosophers Problem in Java
Spring MVC Custom Validation
Spring Cloud Bus
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
A Quick Guide to Spring MVC Matrix Variables