[net.unix] AWK question

ddaly%xls-plexus01.amc@AMC-HQ.ARPA (DUSTY) (08/05/85)

Does anyone know if and how I can get awk to do a >= (less than or equal)
on a value entered from a terminal by the user?
E.G.
echo 'enter date in format yy-mm-dd \c $dt'
read  dt
echo $dt
awk '$5  >= $dt ' .suspfile >xout

awk seems to ignore the terminal entered data. Is there any way to get
awk to recognize this kind of variable?

ed daly
ddaly at amc-hq

ted@ecr2.UUCP (Ted Richards) (08/08/85)

The problem with the statement

	awk '$5 >= $dt' ...

is that the "$dt" is inside single quotes and gets passed literally to awk.
What you want is for the shell to expand the "$dt" (but not the "$5"!).
Try something like:

	echo '$5 >=' $dt >/tmp/whatever
	awk -f /tmp/whatever ...

alex@rruxc.UUCP (A DeSimone) (08/09/85)

> Does anyone know if and how I can get awk to do a >= (less than or equal)
> on a value entered from a terminal by the user?
> E.G.
> echo 'enter date in format yy-mm-dd \c $dt'
> read  dt
> echo $dt
> awk '$5  >= $dt ' .suspfile >xout
> 
> awk seems to ignore the terminal entered data. Is there any way to get
> awk to recognize this kind of variable?
> 
> ed daly
> ddaly at amc-hq


Your problem is not with awk but with the shell's processing
of your command line before passing it on the awk.
The single quotes around the awk command turn off variable substitution
(this is desired for $5 but NOT for $dt).
The solution is to construct a command line that causes the shell
to forego variable substitution for $5 but to perform it for $dt.
The following works for me (SVr2) and should work for you too (the
first 3 lines are yours):

echo 'enter date in format yy-mm-dd \c $dt'
read  dt
echo $dt
# enclose date in double quotes so awk takes it as a string
dt="\"$dt\""
# build awk command line on the fly
awk '$5'" >= $dt " .suspfile >x.out

BTW, ">=" does a *greater than or equal* not *less that or equal* (I
assume this was a typo).  Note that the date you "pass" to awk has
to be in double quotes so that awk will take it as a string rather than a
number.  To avoid further confusion in the command line with still more
double quotes, I've done that in advance of the awk invocation.

->"So it goes."
->Alex DeSimone, Amala Consultants Inc. @ Bell Communications Research
->..!ihnp4!rruxc!alex

ajs@hpfcla.UUCP (ajs) (08/15/85)

Re: getting user input into an AWK program

There are several ways I know of to do this, depending on what exactly
you need to accomplish.

1.  Just hardwire a couple of selected values into the awk script:

    In this case, if the script is invoked on the command line (e.g.
    from a shell script), get the shell to embed the value in the script
    argument.  Normally awk scripts are surrounded by '' in this case,
    so all you have to do is concatenate the value:

	    awk 'BEGIN { value = '$value'; ... }'

    Suppose the awk script needs a string value.  No problem, more quotes:

	    awk 'BEGIN { value = "'$value'"; ... }'

    Suppose the value itself, coming from a user, might contain blanks.
    Uh-oh, better be careful, one more set of quotes:

	    awk 'BEGIN { value = "'"$value"'"; ... }'

    This looks arcane, but is interpretable to any halfway-intelligent
    shell user who READS THE MANUAL at least once.  Any other way to do
    the same thing (like writing the script to a file, or preparing
    $value) is necessarily more complicated and error-prone.

2.  Read lines from input along with other data:

    Make clever use of sh(1) parentheses to combine the data.  Here is
    an example which initializes a relational mapping (pairs of values)
    from one file, then applies it to input data from a user:

		sep='===sep==='		# separator for awk.

	{	cat mapping		# slurp up mapping.
		echo $sep
		cat			# read stdin.
	} |	awk '

		(flag == 0) {			# read mappings.
		    if ($0 == "'"$sep"'")	# end of mapping.
			flag = 1;
		    else
			map [$1] = $2;		# save a mapping.
		
		    next;
		}

		{				# apply mappings to input.
		    if (map [$1] > "")	print map [$1];
		    else		print;
		}'
			
3.  Read lines from stdin or files after awk starts up:

    Well, the 6/5/85 version of awk described by AT&T adds a getline()
    function, among other things.  You'll have to wait for it (as will I).
    Meanwhile you can get the manual --

	> Jon Bentley's Programming Pearls column in CACM (June 1985)
	> discussed awk and mentioned awk functions.  He mentions that
	> the programmer's manual "for the revised language (featurism
	> creeps relentlessly)" is available from ATT.
	> 
	> You can get it too for free by writing:
	> 
	> Patricia Solomon
	> AT&T Bell Laboratories
	> 600 Mountain Avenue
	> Murray Hill, NJ   07974
	> 
	> and asking for:
	> 
	> Computing Science Technical Report: #118
	> Awk Programmer's Manual (May 1985)

Alan Silverstein, Hewlett-Packard Fort Collins Systems Division, Colorado
{ihnp4 | hplabs}!hpfcla!ajs, 303-226-3800 x3053, N 40 31'31" W 105 00'43"

rdkuphal@ihlpm.UUCP (heading) (08/16/85)

> Does anyone know if and how I can get awk to do a >= (less than or equal)
> on a value entered from a terminal by the user?
> E.G.
> echo 'enter date in format yy-mm-dd \c $dt'
> read  dt
> echo $dt
> awk '$5  >= $dt ' .suspfile >xout
> 
> awk seems to ignore the terminal entered data. Is there any way to get
> awk to recognize this kind of variable?
> 
> ed daly
> ddaly at amc-hq

You just past the prompts reply into AWK as a parameter, as follows:

 echo 'enter date in format yy-mm-dd \c $dt'
 read  dt
 echo $dt
 awk '$5  >= $x ' x=$dt .suspfile >xout

The reference to x  may be just  x  instead of $x, I can't remember at
this time.  You may pass as many parameters as you desire, seperated with
white space <SP> .