0

I'm trying to read an output from my awk into a java app, while I know i can read it from an output file (write the awk to the file and then read from that file) this isn't what i'm trying to do. What I was wanting to know is this: is there a way to take my awk output and read it directly into my java (cutting out the middle man) if so what api/awk command would best suit it?

Due to thoughts of duplication, the other question deals with command line. my problem is that i'm using a windows machine, networking to a linux, executing an awk, and wanting to pull the awk response. I'm trying to figure out if 1. that is possible without using an output file 2. if it is possible what would be the right output of the awk, is it print? or would i use the generic shell echo? neither of these are presented on the other page. and 3. which api in java would best suit executing the awk, JSch?

  • 4
    possible duplicate of [java runtime.getruntime() getting output from executing a command line program](http://stackoverflow.com/questions/5711084/java-runtime-getruntime-getting-output-from-executing-a-command-line-program) – ErstwhileIII Aug 01 '14 at 17:35
  • 2
    Would piping via terminal (|) work? – vandale Aug 01 '14 at 17:35
  • 1
    As vandale says... what about "awk ... | java ..." and get the java program read it from the its standard input channel (System.in?) – Valentin Ruano Aug 01 '14 at 17:37
  • You can also use `ProcessBuilder` and read `Process` OutputStream – Sanjeev Aug 01 '14 at 17:51
  • @ErstwhileIII kind of, but not quite, what i am doing is this, i need to remote to a linux server, run an awk on a csv file, pull that output back from the linux and process it on my windows machine. :/ I know how i want to use my awk, but do i use the generic print function of awk and read that using the ProcessBuilder? or is there a better way? i'm new to awk and not sure of the right means of getting it. worst comes to worst i'll just write it to a file, and scp the file over to my machine and read from there. – Jake Hutchinson Aug 01 '14 at 18:16
  • Then give edit your question to give enough details before it is closed as duplicate : as currently writen, it is a duplicate. – Serge Ballesta Aug 01 '14 at 18:32

1 Answers1

0

You have to use a remote exec facility. Jsch with an exec channel is a well known one.

The general way to have it is :

  1. prepare the awk command - use full path and do not rely on environment
  2. execute the command via a Jsch exec channel and send channel output to a ByteArrayOutputStream
  3. read awk output in the underlying String of the ByteArrayOutputStream

Alternatively, you can try to use a pipe, but I do not think that it is necessary.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252