2

I'm probably missing something, but I'm trying to run commandline from a java

The code is as following:

String command = "sed -i 's/\\^@\\^/\\|/g' /tmp/part-00000-00000";
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process process = pb.start();
process.waitFor();
if (process.exitValue() > 0) {
    String output = // get output form command
    throw new Exception(output);
}

I'm getting the following error:

 java.lang.Exception: Cannot run program "sed  -i 's/\^@\^/\|/g' /tmp/part-00000-00000": error=2, No such file or directory

The fils is exists. I'm doing ll on this file and it is exists. I'm just looking for a way to make it work from java. What am I doing wrong?

Julias
  • 5,372
  • 14
  • 53
  • 78
  • 5
    Not too familiar with Java so just a guess but it looks like it's interpreting the whole string as the name of a command, rather than a series of arguments. – Tom Fenech Feb 19 '15 at 15:25
  • Seems like it would be better, to me, to stream the XML in Java and do the search and replace in Java rather than executing `sed` from an external shell – Kirby Dec 17 '15 at 23:27
  • Very similar question here http://stackoverflow.com/q/20232667/266531 – Kirby Dec 17 '15 at 23:50

4 Answers4

6

Pass the command as an array, not a string:

String[] command={"sed", "-i", "'s/\\^@\\^/\\|/g'", "/tmp/part-00000-00000"};

See ProcessBuilder documentation.

sudocode
  • 15,747
  • 38
  • 58
4

Honestly there is no need to externally execute sed in this case. Read the file in Java and use Pattern. Then you have code that could run on any platform. Combine this with org.apache.commons.io.FileUtils and you can do it in a few lines of code.

    final File = new File("/tmp/part-00000-00000");    
    String contents = FileUtils.readFileToString(file, StandardCharsets.UTF_8.name());
    contents = Pattern.compile("\\^@\\^/\\").matcher(contents).replaceAll("|");
    FileUtils.write(file, contents);

Or, in a short, self-contained, correct example

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;

    public final class SedUtil {

        public static void main(String... args) throws Exception {
            final File file = new File("part-00000-00000");
            final String data = "trombone ^@^ shorty";
            FileUtils.write(file, data);
            sed(file, Pattern.compile("\\^@\\^"), "|");
            System.out.println(data);
            System.out.println(FileUtils.readFileToString(file, StandardCharsets.UTF_8));
        }

        public static void sed(File file, Pattern regex, String value) throws IOException {
            String contents = FileUtils.readFileToString(file, StandardCharsets.UTF_8.name());
            contents = regex.matcher(contents).replaceAll(value);
            FileUtils.write(file, contents);
        }
    }

which gives output

trombone ^@^ shorty
trombone | shorty
Kirby
  • 13,024
  • 6
  • 79
  • 95
0

this code here is magic really, simple and short and tested 100% example i want to remove the last character of line from file (/sdcard/MT2/file.json)

String[] cmdline = { "sh", "-c", "sed -i 's/.$//' /sdcard/MT2/file.json " }; 
try {
  Runtime.getRuntime().exec(cmdline);
} catch (Exception s) {
  finishAffinity();
}

this magic code here don't run only sed, but also runnig echo, cat, ....ect good luck

donquih0te
  • 551
  • 3
  • 18
HishamKanon
  • 25
  • 1
  • 6
-1

Try

Process p = Runtime.getRuntime().exec("sed -i 's/\\^@\\^/\\|/g' /tmp/part-00000-00000");
rgrebski
  • 1,728
  • 17
  • 23