Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Grep Library

Tags:

java

grep

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.

like image 312
MJB Avatar asked Jun 03 '11 03:06

MJB


2 Answers

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.

like image 199
Thor Avatar answered Nov 10 '22 07:11

Thor


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.

like image 14
marco Avatar answered Nov 10 '22 09:11

marco