Running Grep command from java -


i want run grep command java.

here had tried. please let me know, why not displaying ouput.

  public static void main(string args[]) throws ioexception{                  runtime rt = runtime.getruntime();             string[] cmd = { "/bin/sh", "-c", "grep 'report process started' server.log|wc -l" };             process proc = rt.exec(cmd);             bufferedreader = new bufferedreader(new inputstreamreader(proc.getinputstream()));             string line;             while ((line = is.readline()) != null) {                 system.out.println(line);             }                        system.out.println("done");               } 

you don't need pipe grep's output wc -l. use grep -c this:

string[] cmd = {"/bin/sh", "-c", "grep -c 'report process started' /path/to/server.log"}; 

though must doing right inside java cleaner. consider code this:

string logdata = new scanner(new file("/path/to/server.log")).usedelimiter("\\z").next(); final string needle = "report process started"; int occurrences = 0; int index = 0; while (index < logdata.length() && (index = logdata.indexof(needle, index)) >= 0) {     occurrences++;     index += needle.length(); } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -