AWK question

A DeSimone alex at rruxc.UUCP
Sat Aug 10 01:10:26 AEST 1985


> 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



More information about the Comp.unix mailing list