Java Program to Implement Grep, Egrep and Fgrep Commands

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.

  1.  
  2. package com.sanfoundry.setandstring;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. public class GrepCommandImplementation
  12. {
  13.     public static void main(String[] argv) throws Exception
  14.     {
  15.         Scanner sc = new Scanner(System.in);
  16.         System.out
  17.                 .println("Enter the string to match from GrepCommandImplementation.java file: ");
  18.         Pattern pattern = Pattern.compile(sc.next());
  19.         Matcher matcher = pattern.matcher("");
  20.         String file = "src/com/sanfoundry/setandstring/GrepCommandImplementation.java";
  21.         BufferedReader br = null;
  22.         String line;
  23.         try
  24.         {
  25.             br = new BufferedReader(new FileReader(file));
  26.         }
  27.         catch (IOException e)
  28.         {
  29.             System.err.println("Cannot read '" + file + "': " + e.getMessage());
  30.         }
  31.         while ((line = br.readLine()) != null)
  32.         {
  33.             matcher.reset(line);
  34.             if (matcher.find())
  35.             {
  36.                 System.out.println(file + ": " + line);
  37.             }
  38.         }
  39.         br.close();
  40.         sc.close();
  41.     }
  42. }

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);

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.