[comp.lang.misc] awk

hal@junkyard.UUCP (Hal Moroff) (04/10/91)

	I'm trying to use awk to read an ascii file, and translate only
certain fields.  I have my own special filter that will be used for the
translation.
	I can get awk to read my input file and extract the desired fields.
I can also get it to invoke my filter passing those fields, however I cannot
get the filter return value assigned to a variable in my awk program.
	My hands are slightly tied in that I cannot modify the way the
filter works.
	An awk code fragment follows:
-------------------------------------------------------------------------------
#
#	awk script to filter selected fields
#
#

BEGIN	{ }

/.*/ {
#	the following statement works whether I use 'cat' or my own filter
	    print ">> " $1  | "cat"

#	however I wish to retrieve the string output by my filter into
#	a variable for further processing by 'awk'.  I've tried:
#	    print ">> " $1  | "cat" | getline

#	and also:
#	    print ">> " $1  | "cat" | getline str

#	and both return the same error:
#awk: syntax error near line 14
#awk: illegal statement near line 14
	}

END	{ }

-------------------------------------------------------------------------------
	Sorry if this distribution is too wide/narrow.  I've read notes
a long time, and posted rarely.
	Reply by email here or to cognex!hal@ai.mit.edu
	Thanx.
					- hal

tchrist@convex.COM (Tom Christiansen) (04/11/91)

From the keyboard of hal@junkyard.UUCP (Hal Moroff):
:
:	I'm trying to use awk to read an ascii file, and translate only
:certain fields.  I have my own special filter that will be used for the
:translation.

This is the old double-headed popen() problem.  Your best
bet is to write it all to a temp file, then read from the
file when done.  This is the best way to avoid deadlock.  

You could theoretically use some forks, dups, and execs
do get this all right, but unless you are sure of the 
behavior of the inferior, you can't guarantee that you'll
be free of deadlock.   Of course, awk won't let you do these
things, nor could you flush the pipe if you wanted to.  
You might look at question 20 of the Frequently Asked
Questions list in comp.lang.perl -- perl code is included
there for doing this, and the algorithm is easily translatable
into C.  But awk users are at a dead-end.

--tom