Jake Hutchinson
Jake Hutchinson

Reputation: 1

How to read an output from awk into your java app

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?

Upvotes: 0

Views: 525

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149125

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.

Upvotes: 0

Related Questions