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:
Guide to the Java ArrayList
Guide to System.gc()
Adding Shutdown Hooks for JVM Applications
Java Program to Implement Sorted List
Unsatisfied Dependency in Spring
Guide to java.util.Formatter
Introduction to Liquibase Rollback
DistinctBy in the Java Stream API
MyBatis with Spring
Java Program to Implement HashMap API
Spring Security Logout
JUnit5 @RunWith
Java Program to Implement Sorted Circularly Singly Linked List
Python Program to Reverse a Number
Introduction to Spring MVC HandlerInterceptor
Spring Security Custom AuthenticationFailureHandler
Java Program to Find Strongly Connected Components in Graphs
Java – Try with Resources
Spring WebClient vs. RestTemplate
Convert Hex to ASCII in Java
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Check whether Graph is a Bipartite using BFS
Java Program to Implement Shell Sort
Giới thiệu JDBC Connection Pool
LIKE Queries in Spring JPA Repositories
Spring Boot - Admin Server
Consuming RESTful Web Services
Python Program to Check Prime Number
Java Program to Implement HashSet API
Guide to java.util.concurrent.Future
Python Program to Check Leap Year
Guide To CompletableFuture