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 Check whether Graph is a Bipartite using DFS
The Modulo Operator in Java
Spring Boot - Securing Web Applications
Spring Boot - Building RESTful Web Services
Checked and Unchecked Exceptions in Java
Ép kiểu trong Java (Type casting)
Send email with SMTPS (eg. Google GMail)
Guide to Spring @Autowired
Python Program to Check the File Size
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Converting Between Byte Arrays and Hexadecimal Strings in Java
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
So sánh HashMap và Hashtable trong Java
Spring Boot - Tomcat Port Number
Java Program to Implement Hash Trie
Hướng dẫn Java Design Pattern – Strategy
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Java Program to Construct an Expression Tree for an Infix Expression
How to Read a Large File Efficiently with Java
A Guide To UDP In Java
Python Program to Safely Create a Nested Directory
Java Program to Check if it is a Sparse Matrix
Java Program to Implement Shell Sort
Hướng dẫn Java Design Pattern – Facade
Java Program to Implement Control Table
Quick Guide on Loading Initial Data with Spring Boot
Lớp HashMap trong Java
Shuffling Collections In Java
Python Program to Extract Extension From the File Name
Guide to java.util.concurrent.Locks
Send email with JavaMail
Java Program to Implement the RSA Algorithm