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:
Từ khóa throw và throws trong Java
Creating a Custom Starter with Spring Boot
Java Program to Implement Sorted Doubly Linked List
Format ZonedDateTime to String
RegEx for matching Date Pattern in Java
Java Program to Implement Sieve Of Atkin
Java Program to Find Inverse of a Matrix
Java Program to Implement the Vigenere Cypher
Zipping Collections in Java
The Java 8 Stream API Tutorial
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Hướng dẫn Java Design Pattern – Facade
Java Program to Compute the Area of a Triangle Using Determinants
Java Program to Implement Heap Sort Using Library Functions
Java Program to Implement Bubble Sort
Hướng dẫn Java Design Pattern – MVC
Java – File to Reader
Giới thiệu SOAP UI và thực hiện test Web Service
Java Program to Implement Kosaraju Algorithm
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Java Program to implement Associate Array
Limiting Query Results with JPA and Spring Data JPA
Java Program to Check the Connectivity of Graph Using DFS
Spring Boot - Batch Service
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Java Program to Implement ScapeGoat Tree
Java Program to Represent Graph Using Incidence Matrix
Java Program to Implement Splay Tree
Java Program to Implement DelayQueue API
Introduction to Apache Commons Text
Guide to Character Encoding
Guide to DelayQueue