我正在运行一个Java代码,该代码sed
在.txt
文件上运行命令以删除Centos上的BOM。我收到以下错误:
sed: -e expression #1, char 1: unknown command: `''
Java代码:
String[] args = {"sed","-i","'1s/^\\xEF\\xBB\\xBF//'","/tmp/output.txt"};
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process p = processBuilder.start();
int exitVal = p.waitFor();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = null;
// read the output from the command
while ((line = stdInput.readLine()) != null) {
output.append(line + "\n");
}
// read any errors from the attempted command
logger.info("Here is the standard error of the command (if any):\n");
while ((line = stdError.readLine()) != null) {
logger.info("Error: " + line);
}
错误消息表明sed
不知道什么命令'
(单引号,表达式中的第一个字符)。
删除sed
表达式周围的单引号。您可能需要在外壳程序中使用它来防止字符串被外壳程序解释,但是sed
从另一种语言调用时,您将不需要它们。
而且,从Java或任何其他语言调用外部工具似乎有点笨拙。我想您可以直接在语言中进行这些修改,可能需要使用某些库。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句