0

I want to run this command using ProcessBuilder:

sort -m -u -T /dir -o output <(zcat big-zipped-file1.gz | sort -u) <(zcat big-zipped-file2.gz | sort -u) <(zcat big-zipped-file3.gz | sort -u) 

I have tried the following:

// This doesn't recognise the redirection.
String[] args = new String[] {"sort", "-m", "-u", "-T", "/dir", "-o", "output", "<(zcat big-zipped-file1.gz | sort -u)", "<(zcat big-zipped-file2.gz | sort -u)", "<(zcat big-zipped-file3.gz | sort -u)"};

// This gives:
// /bin/sh: -c: line 0: syntax error near unexpected token `('
String[] args = new String[] {"/bin/sh", "-c", "\"sort -m -u -T /dir -o output <(zcat big-zipped-file1.gz | sort -u) <(zcat big-zipped-file2.gz | sort -u) <(zcat big-zipped-file3.gz | sort -u)\""};

I am using args like this: processBuilder.command(args);

tourniquet_grab
  • 792
  • 9
  • 14
  • Updated my question. I want to redirect the output from several zcat commands to the sort. – tourniquet_grab Jun 12 '16 at 03:28
  • ProcessBuilder is not a shell. either invoke the shell explicitly or do the redirection yourself. – jtahlborn Jun 12 '16 at 03:59
  • This is not a duplicate. The problem here is different. I did invoke the shell explicitly in my second attempt. – tourniquet_grab Jun 12 '16 at 06:35
  • 2
    First, remove internal quotes around `sort ...`. Second, I don't think `sh` understands `<(...)` syntax - it's more of a `bash` thing. – Roman Jun 12 '16 at 12:05
  • You are correct! I figured this out a few hours after I posted the question but could not add an answer since this question was marked duplicate. – tourniquet_grab Jun 12 '16 at 22:00

1 Answers1

0

I finally figured it out. As Roman has mentioned in his comment, sh does not understand redirection so I had to use bash. I also had to consume both the input stream and the error stream.

String[] args = new String[] {"/bin/bash", "-c", "sort -m -u -T /dir -o output <(zcat big-zipped-file1.gz | sort -u) <(zcat big-zipped-file2.gz | sort -u) <(zcat big-zipped-file3.gz | sort -u)"};

ProcessBuilder builder = new ProcessBuilder();
builder.command(args);
Process process = builder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while((line = input.readLine()) != null);
while((line = error.readLine()) != null);

process.waitFor();
tourniquet_grab
  • 792
  • 9
  • 14