在java中执行awk命令 [英] Executing awk command in java

查看:280
本文介绍了在java中执行awk命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在java中为linux / unix操作系统执行awk命令,但问题是当我执行命令时它没有显示任何错误。但是执行后没有输出,只需要几分之一秒来执行i不知道问题请帮忙。

i am trying to execute awk command in java for linux/unix os but the thing is when i execute the command it does not show any error it.But after execution there is no output and it takes fraction of second to execute i dont know the problem please help .

代码是

process p =new process():
yes = "awk '{print $1}' /root/Desktop/net/net.zone >> /root/Desktop/net/net.txt";
p = Runtime.getRuntime().exec(yes);

感谢您的帮助

推荐答案

使用Java正确启动命令行进程并不容易。我建议您使用 commons-exec 而不是自己尝试。

Starting command line processes correctly with Java isn't easy. I suggest you use commons-exec instead of trying it yourself.

现在命令行中有两件事需要特殊处理:

Now you have two things in the command line which need special handing:


  • 单引号AWK脚本。当您通过 addArgument 将每个参数作为单个字符串传递给 CommandLine 时,您不再需要引号。

  • 输出重定向。

  • Single quotes around the AWK script. When you pass each argument as a individual strings to CommandLine via addArgument, you don't need the quotes anymore.
  • The output redirection.

由于您创建了子进程,因此您可以控制stdin和stout。这意味着您需要打开目标文件以便在Java中追加,将其包装在 PumpStreamHandler 中并将其传递给 DefaultExecutor 。有关详细信息,请参阅此问题: apache-commons exec的处理输出

Since you create a child process, you are in control of stdin and stout. That means you need to open the target file for append in Java, wrap it in a PumpStreamHandler and pass that to DefaultExecutor. See this question for details: Process output from apache-commons exec

这篇关于在java中执行awk命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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