Aditya Grover
Aditya Grover

Reputation: 103

Running pipelined commands using ProcessBuilder in Java

I am trying to extract the frequency of the most frequent line in a file using the following command:

sort file.txt | uniq -c | sort -r | head -1|  xargs

I am trying to accomplish from within a Java program, using the ProcessBuilder class. Here is how I am passing to its constructor:

ProcessBuilder builder=new ProcessBuilder("/bin/sh", "-c","sort",fileName,"| uniq -c | sort -r | head -1 | xargs");

When I run the program, it just stops executing beyond this line. There are no errors, but the program just halts at this line. What is it that I might be doing wrong?

Thanks!

Upvotes: 1

Views: 81

Answers (1)

Ivan Pronin
Ivan Pronin

Reputation: 1896

Try including a filename directly into command:

ProcessBuilder builder=new ProcessBuilder("/bin/sh", "-c","sort " + fileName + " | uniq -c | sort -r | head -1 | xargs");

Upvotes: 1

Related Questions