简体   繁体   中英

Executing awk command in java

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 .

the code is

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

Thank you for your help

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:

  • 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.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM