[comp.unix.xenix] awk

richard@neabbs.UUCP (RICHARD RONTELTAP) (09/29/89)

Help! I need to finish an AWK program yesterday, but can't get it to
pass parameters.
 
For example:
awk 'BEGIN {print x}' x=foo
should print 'foo' and start reading stdin, but it prints an empty
line and terminates.
 
I'm using AWK from XENIX /386 2.2.3. When I type 'awk' without any
arguments, the usage line says nothing about parameters. Maybe it just
isn't built in (yet)?
 
I haven't got the Text Processing System so please tell me anything
usefull you can find in that manual.
 
T.i.a. for comments,
Richard
(...!hp4nl!neabbs!richard)

maart@cs.vu.nl (Maarten Litmaath) (09/29/89)

richard@neabbs.UUCP (RICHARD RONTELTAP) writes:
\...
\awk 'BEGIN {print x}' x=foo
\should print 'foo' and start reading stdin, but it prints an empty
\line and terminates.

Known bug: the variable is only set AFTER the first input line has been
read. :-(
Work-around:

	(echo ''; cat) | awk 'NR == 1 {print x}' x=foo -

Don't forget to add the `-' (for stdin), else you'll step into another
bug... :-(
-- 
 `I AM NEW HEAR AMD I WANT TO INKRIMENT A |Maarten Litmaath @ VU Amsterdam:
 VURIABLE BY 1 (OONE) IN "c"'  (Tom Neff) |maart@cs.vu.nl, mcvax!botter!maart

root@ozdaltx.UUCP (root) (09/30/89)

In article <218001@neabbs.UUCP>, richard@neabbs.UUCP (RICHARD RONTELTAP) writes:
> Help! I need to finish an AWK program yesterday, but can't get it to
> pass parameters.
>  
> For example:
> awk 'BEGIN {print x}' x=foo
> should print 'foo' and start reading stdin, but it prints an empty
> line and terminates.
>  

Remember that awk works on a record by record basis, each
record terminated by a newline unless told otherwise.

In your example, it probably would be as effective to do
something like:

   (echo $x; awk '{...command list .....}') 

The parenthesis keep the shell from "seeing" the two
commands as being different and treats them as one command.

One of the best books detailing awk is called the UNIX
PROGRAMMING ENVIROMENT by Kerningon & Pike.  Pretty well
explains everything you'd want to know.

good luck....
         Scotty

richard@neabbs.UUCP (RICHARD RONTELTAP) (10/02/89)

> > awk 'BEGIN {print x}' x=foo                                                  
...
> In your example, it probably would be as effective to do                       
> something like:                                                                
>                                                                               
>   (echo $x; awk '{...command list .....}')                                    

This will work for the example, but that was only an example to show
the bug. The real AWK program is used to purge dates from continuously
incoming news...

However, someone else sent me a workaround. Thank's! I can't find
your name rigth now, because I'm at another location. From memory,
the workaround was that variables are only initialised after the
first line has been read. You also have to specify a minus sign on
the command line to force reading stdin. (Which should be standard)

I was also very 'charmed' by an offer from SCO. If I upgraded to
XENIX 2.3 (currenly have 2.2.3) or SCO UNIX, AWK would work as
documented. A bit expensive for a bug-fix. :-)

(SCO UNIX is, of course, very attractive for other reasons.)

Thanks again, unknown,
Greetings,
Richard
(...!hp4nl!neabbs!richard)

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (10/03/89)

In article <219915@neabbs.UUCP>, richard@neabbs.UUCP (RICHARD RONTELTAP) writes:

|  However, someone else sent me a workaround. Thank's! I can't find
|  your name rigth now, because I'm at another location. From memory,
|  the workaround was that variables are only initialised after the
|  first line has been read. You also have to specify a minus sign on
|  the command line to force reading stdin. (Which should be standard)

  It seems to work in 2.3.2. You could do that update, which might be
cheaper, or install gawk which should fix the problem. The required -
seems to have gone away in 2.3.2, also. I have a lot of scripts which
include it, but I tried your example, thing the - was the problem, and
it worked either way. Also works with the new AT&T awk.
-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon