23

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.

4 Answers 4

16

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That's more or less what I wrote in the end, except I used the very nice FileUtils.listFiles to filter stuff.
15

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.

Comments

8

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

3 Comments

fyi, not a pure-java grep, it requires grep running on the machine.
@jk. Will you be able to tell me if it works with windows version of grep as well?
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.
4

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

1 Comment

You misspelled Pattern. Fix it, this is the right answer. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.