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:
Finding Max/Min of a List or Collection
Java Program to Solve any Linear Equation in One Variable
Spring REST API + OAuth2 + Angular
Sử dụng CountDownLatch trong Java
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Finding the Differences Between Two Lists in Java
Spring WebClient Requests with Parameters
Java Program to Implement Suffix Array
Python Program to Parse a String to a Float or Int
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Spring Autowiring of Generic Types
Java Program to Construct an Expression Tree for an Postfix Expression
Python Program to Find ASCII Value of Character
How to Read HTTP Headers in Spring REST Controllers
How to Kill a Java Thread
Overview of Spring Boot Dev Tools
Java Program to Compute Cross Product of Two Vectors
A Guide to TreeMap in Java
Java Program to Perform LU Decomposition of any Matrix
Java Program to Represent Graph Using Linked List
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Introduction to Spring Boot CLI
More Jackson Annotations
How to Define a Spring Boot Filter?
Number Formatting in Java
Spring Boot - Internationalization
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Optional as Return Type
Spring Boot - Enabling HTTPS
Java Program to Implement Merge Sort Algorithm on Linked List
Java Program to Implement Karatsuba Multiplication Algorithm
Disable DNS caching