22

Is there any good, simple Java Grep Library? I'm not opposed to native code, or scripting, and I'll do it, but for my purposes, throughput is not a huge deal, and it would be nice to have it all in one tidy package.

UPDATE: Sorry. I know about java.regex, I just happen to be fairly busy and tired right now. What I'm looking for is something that efficiently combines java regex with going through a set of files and rewriting them. This wouldn't be too hard to write, I admit. I was just curious if that exists already.

MJB
  • 9,352
  • 6
  • 34
  • 49

4 Answers4

15

I'm not aware of a sophisticated grep librarystrong text, but you are right: it's not hard to write. I suggest a combination of commons-io and String.matches(someRegex):

public class Grep extends DirectoryWalker
{
    public Grep(){
        super();
    }

    public List clean(File startDirectory){
      List results = new ArrayList();
      walk(startDirectory, results);
      return results;
    }

    protected boolean handleDirectory(File directory,
                                      int depth, Collection results){
      // Decide if a (sub) directory will be handled for recursive search
      return true;
    }

    protected void handleFile(File file, int depth, Collection results)
    {
        LineIterator it = FileUtils.lineIterator(file, "UTF-8");
        try{
            while (it.hasNext()){
                String line = it.nextLine();
                if(line.matches("myRegEx")){
                    results.add(file);
                }
            }
         }
         finally {LineIterator.closeQuietly(it);}
    }
}

Update Marco pointed out Unix4j which is a quite interesting library which emulates the unix pipelining | with Java method chaining. grep is supported as well as cat, cd, cut, echo, find, grep, head, ls, sed, sort, tail, uniq, wc, barges.

Thor
  • 6,607
  • 13
  • 62
  • 96
  • That's more or less what I wrote in the end, except I used the very nice FileUtils.listFiles to filter stuff. – MJB Jun 03 '11 at 06:39
14

Unix4j also implements a (pure java) grep command: http://www.unix4j.org

Unix4j.fromStrings("1:A", "2:B", "3:AB", "4:AC", "5:ABC").toFile("myFile.txt");
Unix4j.fromFile("myFile.txt").grep("AB").toStdOut();

>>>
3:AB 
5:ABC

Disclosure: I am one of the contributors to the unix4j project.

marco
  • 711
  • 6
  • 14
8

yep. Grep4j - a grep lib for Unix environments, and you can also grep remotely easy : http://code.google.com/p/grep4j/

FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79
marcocast
  • 81
  • 1
  • 1
  • 2
    fyi, not a pure-java grep, it requires grep running on the machine. – jk. Sep 10 '12 at 22:38
  • @jk. Will you be able to tell me if it works with windows version of grep as well? – Raja Anbazhagan Dec 17 '14 at 15:50
  • I had problem with using grep4j 1.8.7 coming from maven central repository. It used very old version of Lombok and my project used newest. When building project first time, some compilation errors occurred, which disappeared in re-builds. – ilvez Sep 22 '15 at 12:23
4

String.matches(someRegex); Internally uses java.util.regex.Pattern and Matcher

Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53