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 Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Getting a File’s Mime Type in Java
Từ khóa throw và throws trong Java
Java Program to Solve any Linear Equation in One Variable
Spring Boot Annotations
Uploading MultipartFile with Spring RestTemplate
Find the Registered Spring Security Filters
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Auditing with JPA, Hibernate, and Spring Data JPA
Chuyển đổi giữa các kiểu dữ liệu trong Java
An Introduction to Java.util.Hashtable Class
A Guide to HashSet in Java
Consuming RESTful Web Services
Java Program to Implement Levenshtein Distance Computing Algorithm
Simple Single Sign-On with Spring Security OAuth2
Setting a Request Timeout for a Spring REST API
Sending Emails with Java
Send an email using the SMTP protocol
Function trong Java 8
Java Program to Implement Heap Sort Using Library Functions
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement Hamiltonian Cycle Algorithm
How to Convert List to Map in Java
Java Program to Generate Random Hexadecimal Byte
Redirect to Different Pages after Login with Spring Security
A Guide to LinkedHashMap in Java
Java – Reader to String
Java Program to Implement Sparse Array
Semaphore trong Java
Đồng bộ hóa các luồng trong Java
Spring Security Login Page with React
The Registration Process With Spring Security