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 Queue
Java Program to Create the Prufer Code for a Tree
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Encode/Decode to/from Base64
Retrieve User Information in Spring Security
Python Program to Find Hash of File
Converting a Stack Trace to a String in Java
Python Program to Find the Factorial of a Number
@Lookup Annotation in Spring
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Spring 5 WebClient
Command-Line Arguments in Java
Spring Security OAuth Login with WebFlux
Shuffling Collections In Java
Number Formatting in Java
Java Program to Perform Quick Sort on Large Number of Elements
Java Program to Implement Stack API
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Changing Annotation Parameters At Runtime
Python Program to Merge Mails
Giới thiệu Java 8
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Send email with SMTPS (eg. Google GMail)
Python Program to Display Calendar
Exploring the Spring 5 WebFlux URL Matching
Introduction to Spring Cloud CLI
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Implement Hamiltonian Cycle Algorithm
Guide to Apache Commons CircularFifoQueue
HandlerAdapters in Spring MVC
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
A Guide to @RepeatedTest in Junit 5