sed command not working from java

cHam

I am trying to run a sed command from 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);

Thing is, if I print out the contents of the cmd String and run it in a terminal it does work. It's just not executing it from java for some reason. Te make this more clear, when I run the command directly from a terminal the file "items.xml" changes. When I run it from java the file does not change. I've verified that the command is correct as sown below.

Am I missing something?

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

** EDIT

I made the following changes based on comments below. No change in output however.

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();
L. Quastana

Try that :)

The advantage of this solution , it's more easier to debugging because you have the temporary file !

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());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

sed command not working on Centos in java code

From Dev

How to run sed command from java code

From Dev

How to run sed command from java code

From Dev

Sed command not working as expected

From Dev

why is this sed command not working?

From Dev

Sed command not working as expected for delete particular line from file

From Dev

sed command is working in solaris but not in Linux

From Dev

In-place sed command not working

From Dev

SED not working [unterminated `s' command]

From Dev

Sed command to clean a csv is not working

From Dev

SED command not replacing (working regex)

From Dev

sed command not working with search string

From Dev

SED not working [unterminated `s' command]

From Dev

Sed command not working through SSH

From Dev

Remote SSH command with sed not working

From Dev

sed command containing variable not working

From Dev

sed's 'N' command working intermittently

From Dev

sed multiple statements within a single command not working

From Dev

Sed command not working in sun solaris machine

From Dev

sed command working only when no outfile is written

From Dev

sed command not working while executing C file

From Dev

escaping sed characters java not working

From Dev

Run a batch file from Task Scheduler is not working with a java command

From Dev

Windows REG command not working when executed from ProcessBuilder in Java

From Dev

sed command to replace newline character is not working in solaris but working in linux

From Dev

Removing lines from multiple files with sed command

From Dev

SED command not being run from bash script

From Dev

porting sed command from linux to mac

From Dev

Using sed to color the output from a command on solaris

Related Related

HotTag

Archive