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 Unrolled Linked List
Spring Autowiring of Generic Types
Spring’s RequestBody and ResponseBody Annotations
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Implement the RSA Algorithm
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Simplify the DAO with Spring and Java Generics
Java Program to Implement Aho-Corasick Algorithm for String Matching
Intro to Inversion of Control and Dependency Injection with Spring
Hướng dẫn sử dụng Printing Service trong Java
Lập trình mạng với java
Hướng dẫn Java Design Pattern – Template Method
Mix plain text and HTML content in a mail
Giới thiệu luồng vào ra (I/O) trong Java
Python Program to Solve Quadratic Equation
Java – Write a Reader to File
Java Program to implement Circular Buffer
A Guide to Java HashMap
Java Program to Implement Expression Tree
Logout in an OAuth Secured Application
Java Program to Solve a Matching Problem for a Given Specific Case
Registration – Password Strength and Rules
Hướng dẫn sử dụng Java Generics
Tìm hiểu về Web Service
Java Program to Implement Bit Array
Split a String in Java
Lập trình đa luồng trong Java (Java Multi-threading)
Spring Boot - Creating Docker Image
Serve Static Resources with Spring
Java Program to Implement Cubic convergence 1/pi Algorithm
Java Program to Find Nearest Neighbor Using Linear Search
How to Return 404 with Spring WebFlux