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:
Python Program to Display Fibonacci Sequence Using Recursion
Spring Boot Change Context Path
Dynamic Proxies in Java
Python Program to Check the File Size
Python Program to Trim Whitespace From a String
Java Program to Find a Good Feedback Edge Set in a Graph
Java Program to Encode a Message Using Playfair Cipher
Java – Convert File to InputStream
Remove All Occurrences of a Specific Value from a List
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Java Program to Find Basis and Dimension of a Matrix
Constructor Injection in Spring with Lombok
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java Program to Implement Sorted List
Toán tử instanceof trong java
Java Program to Find Number of Articulation points in a Graph
Java Program to Implement Lloyd’s Algorithm
Setting a Request Timeout for a Spring REST API
Python Program to Merge Two Dictionaries
Guide to Spring @Autowired
Java Program to Implement Knapsack Algorithm
Custom Thread Pools In Java 8 Parallel Streams
Quick Guide to Spring Controllers
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Program to Implement Disjoint Sets
Java Program to Implement Iterative Deepening
Comparing Two HashMaps in Java
Partition a List in Java
Guide to Spring 5 WebFlux
Immutable Map Implementations in Java
Exception Handling in Java