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:
Exploring the Spring 5 WebFlux URL Matching
Java Program to Implement HashSet API
New Features in Java 13
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Spring Data MongoDB Transactions
Java Program to Implement LinkedHashSet API
Introduction to Liquibase Rollback
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Updating your Password
Implementing a Binary Tree in Java
Python Program to Find the Size (Resolution) of a Image
Python Program to Find HCF or GCD
Java Program to Implement a Binary Search Tree using Linked Lists
Allow user:password in URL
Giới thiệu java.io.tmpdir
Spring Boot - Unit Test Cases
Spring Boot With H2 Database
Transaction Propagation and Isolation in Spring @Transactional
Guide to BufferedReader
Converting Strings to Enums in Java
How to Use if/else Logic in Java 8 Streams
Java Program to Implement Disjoint Set Data Structure
Spring Cloud AWS – RDS
Java Program to Implement TreeSet API
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
REST Web service: Basic Authentication trong Jersey 2.x
Remove All Occurrences of a Specific Value from a List
Consumer trong Java 8
Java Program to Represent Linear Equations in Matrix Form
Jackson Exceptions – Problems and Solutions