Sed command not working from java

I am trying to run a command sedfrom java without success. Here is my java code:

String[] cmd = {"sed", "-i", "'"+lineIndex+"s/"+line+"/"+currentBid+"/g'", "/data/jsp/items.xml"};
        Runtime.getRuntime().exec(cmd);

I also tried:

String[] cmd = {"/bin/sh","-c","sed", "-i", "'"+lineIndex+"s/"+line+"/"+currentBid+"/g'", "/data/jsp/items.xml"};
        Runtime.getRuntime().exec(cmd);

The thing is, if I print the contents cmd Stringand run it in the terminal, it really works. For some reason, it just doesn't execute from java. That makes this clearer, when I run the command directly from the terminal, the "items.xml" file changes. When I run it from java, the file does not change. I checked that the command is correct as shown below.

Did I miss something?

The output from cmd is sed -i '21s/2/102/g' /data/jsp/items.xml

** EDIT

I made the following changes based on the comments below. However, no change in output.

String[] cmd = {"/bin/sh","-c","sed", "-i", "'"+lineIndex+"s/"+line+"/"+currentBid+"/g'", "/data/jsp/items.xml"};
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();

BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line2 = reader.readLine();
while (line2 != null) {
     line2 = reader.readLine();
}
reader.close();
+2
source share
4 answers

:

- , !

String lineIndex="21";
String line="2";
String currentBid="102";

File temp = File.createTempFile("temp-sh", ".sh"); 



FileWriter fw = new FileWriter(temp);
fw.write("#!/bin/bash\n");
fw.write("sed -i '"+lineIndex+"s/"+line+"/"+currentBid+"/g' data/jsp/items.xml\n");
fw.close();
System.out.println(". "+temp.getAbsolutePath());
Runtime.getRuntime().exec(". "+temp.getAbsolutePath());
0

, ProcessBuilder Runtime.exec, , - -

try {
  String replaceCommand ="'"+lineIndex+"s/"+line+"/"+currentBid+"/g'";
  String [] cmd = new String[] {
      "sed", "-i", replaceCommand, "/data/jsp/items.xml"  
  };
  Process process = new ProcessBuilder(cmd)
      .start();
  InputStream is = process.getInputStream();
  InputStreamReader isr = new InputStreamReader(is);
  BufferedReader br = new BufferedReader(isr);
  String lineRead;

  System.out.printf("Output of running %s is:",
      Arrays.toString(cmd));

  while ((lineRead = br.readLine()) != null) {
    System.out.println(lineRead);
  }
} catch (IOException e) {
  e.printStackTrace();
}
+1

, , java , . , , , .

. Java-? java.io.File

+1

Honestly, in this case there is no need to execute sedfrom the outside. Read the file in Java and use Pattern. Then you have code that can work on any platform. Combine this with org.apache.commons.io.FileUtilsand you can do this in a few lines of code.

    final File = new File("/data/jsp/items.xml");    
    String contents = FileUtils.readFileToString(file, StandardCharsets.UTF_8.name());
    contents = Pattern.compile(line).matcher(contents).replaceAll(currentBid);
    FileUtils.write(file, contents);

Or, in a short, self-sufficient, 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("items.xml");
        final String line = "<bid>\\d+</bid>";
        final String currentBid = "<bid>20</bid>";
        final String data = "<bids><bid>10</bid></bids>";
        FileUtils.write(file, data);
        sed(file, Pattern.compile(line), currentBid);
        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 way

<bids><bid>10</bid></bids>
<bids><bid>20</bid></bids>
0
source

Source: https://habr.com/ru/post/1679623/


All Articles