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:
Spring Boot - Code Structure
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Generate a Random Subset by Coin Flipping
Java Program to Solve Tower of Hanoi Problem using Stacks
Java Program to Implement Floyd-Warshall Algorithm
Java Program to Represent Graph Using Linked List
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Spring – Injecting Collections
Java Program to Implement Sieve Of Eratosthenes
Java Program to Implement Sparse Array
Java Program to Compute Cross Product of Two Vectors
Python Program to Return Multiple Values From a Function
Java – Byte Array to Reader
Generate Spring Boot REST Client with Swagger
Python Program to Create a Countdown Timer
Limiting Query Results with JPA and Spring Data JPA
Giới thiệu HATEOAS
So sánh ArrayList và LinkedList trong Java
Java – File to Reader
Các nguyên lý thiết kế hướng đối tượng – SOLID
Jackson JSON Views
Call Methods at Runtime Using Java Reflection
Java String to InputStream
Java Program to Implement Pairing Heap
Java Program to Implement Hash Tables Chaining with List Heads
Spring Boot - Logging
Guide to Guava Multimap
Hướng dẫn Java Design Pattern – Abstract Factory
Java Program to Create the Prufer Code for a Tree
Java Program to Implement Patricia Trie
Java – InputStream to Reader
Python Program to Get the Last Element of the List