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:
Ép kiểu trong Java (Type casting)
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Java Program to Perform the Shaker Sort
Java Program to Find Number of Articulation points in a Graph
An Introduction to Java.util.Hashtable Class
Java Program to implement Bi Directional Map
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Java Program to Implement LinkedHashSet API
ETL with Spring Cloud Data Flow
Java Program to Implement Quick Sort with Given Complexity Constraint
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Hướng dẫn Java Design Pattern – Singleton
Hướng dẫn Java Design Pattern – DAO
Generic Constructors in Java
Lớp Collections trong Java (Collections Utility Class)
Java Program to Implement DelayQueue API
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Python Program to Check the File Size
String Initialization in Java
Java Program to Implement LinkedBlockingDeque API
Life Cycle of a Thread in Java
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Java Program to Implement Knapsack Algorithm
Spring Data JPA Delete and Relationships
The Thread.join() Method in Java
How to Round a Number to N Decimal Places in Java
Constructor Dependency Injection in Spring
Guide to Spring @Autowired
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Encode a Message Using Playfair Cipher
Spring Security Basic Authentication
Java Program to Implement Treap