从应用程序调用时SED命令不生成输出文件 [英] SED command not generating output file when called from app

查看:575
本文介绍了从应用程序调用时SED命令不生成输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个Java程序,需要字符串Java和替换文本的相应序列在Perl脚本。这里是我的code:

I am trying to write a java program that takes Strings in Java and replaces corresponding sequences of text in a perl script. Here is my code:

    String sedFirstLine = "'s/AAA/"+newFirstLine+"/'";
    String sedNewCntr = "'s/BBB/"+newCntr+"/'";
    String sedNewSpacing = "'s/SPACE/"+newSpacing+"/'";
    String sedNewDmax = "'s/MAX/"+newDmax+"/'";
    String sedInputFile = "/filepath/myPerlScript.pl" 
    String sedOutputFile = "/filepath/myNewPerlScript.pl";
    String[] cmdArray3 = {"sed", "-e", sedFirstLine,"-e", sedNewCntr,"-e", sedNewSpacing,"-e", sedNewDmax, "-e", sedInputFile, ">", sedOutputFile};
    Process runCmd;
    runCmd = Runtime.getRuntime().exec(cmdArray3);

当我运行这个程序,是不是生成的输出文件myNewPerlScript.pl。我不知道什么是错的,我写的东西。我指的是早期的Java变量是newFirstLine,newCntr等。

When I run this program, the output file "myNewPerlScript.pl" is not generated. I'm not sure what is wrong with what I've written. The Java variables that I was referring to earlier are "newFirstLine", "newCntr", etc.

推荐答案

这是因为输出重定向(即> )是shell的功能。为了重定向命令的输出,你可以用一个shell调用命令。例如,使用此命令

This is because the output redirection (i.e. the > ) is a functionality of the shell. In order to redirect the output of the command, you can invoke the command through a shell. For example, with this command

String[] cmdArray3 = {"bash", "-c", "sed 's/AAA/BBB/' inputfile > output"};
Process runCmd = Runtime.getRuntime().exec(cmdArray3);

如预期输出文件应该被创建。

the output file should be created as expected.

这篇关于从应用程序调用时SED命令不生成输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆