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 – InputStream to Reader
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Spring Data Reactive Repositories with MongoDB
Spring Data JPA @Query
Java Program to Implement Iterative Deepening
Java Program to Implement Naor-Reingold Pseudo Random Function
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Ép kiểu trong Java (Type casting)
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Java Program to Implement Patricia Trie
Intro to Inversion of Control and Dependency Injection with Spring
Python Program to Find HCF or GCD
Java Program to Implement Suffix Tree
Pagination and Sorting using Spring Data JPA
Most commonly used String methods in Java
HashSet trong java
Giới thiệu Design Patterns
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Java Program to Implement Floyd-Warshall Algorithm
The DAO with Spring and Hibernate
Tránh lỗi NullPointerException trong Java như thế nào?
Abstract class và Interface trong Java
String Processing with Apache Commons Lang 3
Guide to System.gc()
A Guide to Concurrent Queues in Java
Java Program to Find the Connected Components of an UnDirected Graph
Python Program to Get Line Count of a File
A Quick Guide to Spring MVC Matrix Variables
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Java Program to Implement Lloyd’s Algorithm
Java Program to Implement Knapsack Algorithm
Validate email address exists or not by Java Code