简体   繁体   中英

How to run sed command from java code

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?

Pass the command as an array, not a string:

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

See ProcessBuilder documentation.

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

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

尝试

Process p = Runtime.getRuntime().exec("sed -i 's/\\^@\\^/\\|/g' /tmp/part-00000-00000");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM