I was trying to run a unix command in java to overlook the double quotation mark in a parsed file:
for(int i = 0; i < numTables; i++){
try{
String command = "sed -e \'s/\"/\"\"/g\' -e \'s/^/\"/\' -e \'s/$/\"/\' -e \'s/<>/\"<>\"/g\' input.dat > output.dat";
Process p = Runtime.getRuntime().exec(command);
} catch(IOException ioe){
System.out.println("Error executing command");
}
}
However, typing the identical command on the terminal directly would work. Any idea what went wrong? Thank you!
Update: In fact, I tried the following (using array instead of just a String), it failed too:
String[] command = new String[] {"sed", "-e", "\'s/\"/\"\"/g\'", "-e", "\'s/^/\"/\'", "-e", "\'s/$/\"/\'", "-e", "\'s/<>/\"<>\"/g\'", prefixedFileList.get(i), ">", fileList.get(i)};
Process p = Runtime.getRuntime().exec(command);
Any thoughts?
As a clearer picture, the corresponding plain text that would be executed on unix terminal would be
sed -e 's/"/""/g' -e 's/^/"/' -e 's/$/"/' -e 's/<>/"<>"/g' input.dat > output.dat