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:
Xây dựng ứng dụng Client-Server với Socket trong Java
Exploring the Spring 5 WebFlux URL Matching
Java Program to Implement Extended Euclid Algorithm
Logging in Spring Boot
Java Program to Implement Unrolled Linked List
Documenting a Spring REST API Using OpenAPI 3.0
An Intro to Spring Cloud Contract
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Spring RestTemplate Request/Response Logging
Spring Boot Actuator
Spring WebClient vs. RestTemplate
A Guide to Concurrent Queues in Java
Send email with authentication
Inheritance and Composition (Is-a vs Has-a relationship) in Java
JPA/Hibernate Persistence Context
Remove HTML tags from a file to extract only the TEXT
Compare Two JSON Objects with Jackson
Using JWT with Spring Security OAuth (legacy stack)
Spring Security – security none, filters none, access permitAll
Disable DNS caching
Collect a Java Stream to an Immutable Collection
Java Program to Implement the Vigenere Cypher
Java Program to Implement PrinterStateReasons API
Từ khóa this và super trong Java
Java Program to Implement Sorted Circularly Singly Linked List
Python Program to Represent enum
Hướng dẫn Java Design Pattern – Template Method
Encode a String to UTF-8 in Java
Lấy ngày giờ hiện tại trong Java
New Features in Java 11
Tạo chương trình Java đầu tiên sử dụng Eclipse