[comp.unix.wizards] awk arguments

mayne@VSSERV.SCRI.FSU.EDU (William (Bill) Mayne) (07/23/90)

I have had a problem with the syntax of the awk command for some
time. I quote from the man pages for awk from SunOS:
> 
> SYNOPSIS
>      awk [ -f program-file ] [ -Fc  ]  [  program  ]  [  variable
>      =value ... ] [ filename...]
> 
Since unix file names may contain an = this is clearly ambiguous.
Does

      awk '{print $0; print $Y}' Y=X

print lines from standard input separated by a line containing "X"
or does it print lines from the file Y=X separated by blank lines,
since without the assignment the value Y in awk should be null?
Either interpretation would be justified by the syntax. 
The command could be parsed where Y=X is either [variable=value]
or [filename...]. In fact it does neither. In this case, whether a
file named Y=X exists or not, standard input is not read and
nothing is printed.

The practical problem this raises is how to communicate an
argument value or a value calculated by a script file into
an awk program embedded in a script file. Referencing $n in
the quoted awk program won't work, since awk will think that
refers to field number n. Similarly any other reference to a
shell variable will be interpreted as a reference to an awk
variable.

I have never been able to get the command line assignment of 
awk variables to work. I have sometimes resorted to ugly kludges 
like using sed to modify an awk program to hard code argument 
values. Surely I have missed something obvious. There must be a 
better way. I would be most grateful if someone would suggest one.

While I am on the subject of awk: I learned about the language
from the book "The AWK Programming Language" by Kernighan et. al.
before I started working on unix. I have been disappointed to
find that the version actually available on every flavor of unix
I have seen is much weaker than the full version described by the
book. Are there better versions out there?

omerzu@quando.quantum.de (Thomas Omerzu) (07/24/90)

In article <290@sun13.scri.fsu.edu>
mayne@VSSERV.SCRI.FSU.EDU (William (Bill) Mayne) writes:

| Does
|       awk '{print $0; print $Y}' Y=X
| print lines from standard input separated by a line containing "X"
| or does it print lines from the file Y=X separated by blank lines,
| since without the assignment the value Y in awk should be null?
[...]
| In fact it does neither. In this case, whether a
| file named Y=X exists or not, standard input is not read and
| nothing is printed.

Obviously awk thinks that filenames are present
if _anything_ follows after the program;
in that case the standard input is not read.

You can avoid this by using '-' as filename argument.

If you add a '-' to your example, it will still not work.
Actually varibles are referenced in awk programs _without_ 
a leading '$'.

Thus
      awk '{print $0; print Y}' Y=X -

will do the job:
read standard input an print contents of every line followed by
a line containing the value of 'Y'.


| I have never been able to get the command line assignment of 
| awk variables to work. I have sometimes resorted to ugly kludges 
| like using sed to modify an awk program to hard code argument 
| values. Surely I have missed something obvious.

exactly: it was the '$'. :-)


| While I am on the subject of awk: I learned about the language
| from the book "The AWK Programming Language" by Kernighan et. al.
| before I started working on unix. I have been disappointed to
| find that the version actually available on every flavor of unix
| I have seen is much weaker than the full version described by the
| book. Are there better versions out there?

Well, I don't know that book
and thus I don't know what weaknesses you're pointing at.
Maybe you could tell us something more specific?

I've always been quite satisfied with the
abilities of 'awk'

- except of one point: the error checking of program scripts.
You'll nearly ever get 'bailing out near line ...'.
That point would have been worth of being mentioned in the
'bug' section of the manual for awk.



-- 
*-----------------------------------------------------------------------------*
Thomas Omerzu      UUCP:     ...!unido!quando!omerzu / omerzu@quando.uucp
  Quantum GmbH,    Bitnet:   UNIDO!quando!omerzu / omerzu%quando@UNIDO(.bitnet)
Dortmund, Germany  Internet: omerzu@quando.quantum.de

davidsen@antarctica.crd.GE.COM (william E Davidsen) (07/25/90)

To: 
Cc: 
Subject: 


In article <78@cdss.UUCP>, lyons@cdss.UUCP (Don R. Lyons x4811) writes:
|> Can anyone tell me why the form widget does not size to fit
|> button1, button2, button3 under SCO-ODT? When this application is
|> displayed only button1 and button2 are visible. All three are visible
|> if don't set XmNbottomAttachment for wbutton[2]. Please email me, I
|> have a product deadline I am trying to meet.
|> 
|> thanx
|>   -don
|> 
|> 
|> 	#include <X11/Intrinsic.h>
|> 	#include <Xm/Xm.h>
|> 	#include <Xm/Form.h>
|> 	#include <Xm/PushB.h>
|> 	
|> 	char *buttons[] = { "button1", "button2", "button3" };
|> 	
|> 	void main( argc, argv)
|> 	int argc;
|> 	char **argv;
|> 	{
|> 	  Widget toplevel, form, wbutton[5];
|> 	  int i, n;
|> 	  Arg wargs[10];
|> 	
|> 	  toplevel = XtInitialize( argv[0], "Formtest", NULL, 0, &argc, argv);
|> 	
|> 	  form = XtCreateManagedWidget( "form", xmFormWidgetClass, toplevel,
|> 						NULL, 0);
|> 	
|> 	  for ( i = 0; i < XtNumber(buttons); i++)
|> 	    wbutton[i] = XtCreateWidget( buttons[i],
|> 	   			 xmPushButtonWidgetClass, form, NULL, 0);
|> 	  XtManageChildren( wbutton, XtNumber(buttons));
|> 	
|> 	  n = 0;
|> 	  XtSetArg(wargs[n], XmNtopAttachment,   XmATTACH_FORM); n++;
|> 	  XtSetArg(wargs[n], XmNleftAttachment,  XmATTACH_FORM); n++;
|> 	  XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM); n++;
|> 	  XtSetValues(wbutton[0], wargs, n);
|> 	  n = 0;
|> 	  XtSetArg(wargs[n], XmNtopAttachment,   XmATTACH_WIDGET);n++;
|> 	  XtSetArg(wargs[n], XmNtopWidget,       wbutton[0]);     n++;
|> 	  XtSetArg(wargs[n], XmNleftAttachment,  XmATTACH_FORM);  n++;
|> 	  XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM);  n++;
|> 	  XtSetValues(wbutton[1], wargs, n);
|> 	  n = 0;
|> 	  XtSetArg(wargs[n], XmNtopAttachment,   XmATTACH_WIDGET);n++;
|> 	  XtSetArg(wargs[n], XmNtopWidget,       wbutton[1]);     n++;
|> 	  XtSetArg(wargs[n], XmNleftAttachment,  XmATTACH_FORM);  n++;
|> 	  XtSetArg(wargs[n], XmNrightAttachment, XmATTACH_FORM);  n++;
|> 	  XtSetArg(wargs[n], XmNbottomAttachment,XmATTACH_FORM);  n++;
|> 	  XtSetValues(wbutton[2], wargs, n);
|> 	
|> 	  XtRealizeWidget(toplevel);
|> 	  XtMainLoop();
|> 	}
|> -- 
|> Don R Lyons                          Any opinions expressed are my own.
|> Arinc Research Inc                      uucp : uunet!cdss!lyons
|> CDSS, Mail Stop 1249                    voice: 301 266 4811
|> 2551 Riva Road Annapolis , MD 21401     fax  : 301 266 2047
Bill Davidsen (davidsen@crdos1.crd.ge.com, uunet!crdgw1!crdos1!davidsen)
  GE Corp R&D Center, Schenectady NY
  Moderator of comp.binaries.ibm.pc and 386users mailing list
"Stupidity, like virtue, is it's own reward" -me


Path: antarctica!davidsen
Newsgroups:
comp.periphs.scsi,ba.market.computers,comp.sys.ibm.pc.hardware,comp.sys.
.mac.hardware
Distribution: world
Followup-To: 
References: <5278@darkstar.ucsc.edu> <31926@cup.portal.com>
From: davidsen@antarctica.crd.GE.COM (william E Davidsen)
Reply-To: davidsen@crdos1.crd.ge.com (bill davidsen)
Organization: GE Corporate R&D Center, Schenectady NY
Subject: Re: New 320Meg SCSIs  < $1000 (quantity purchase?)
Keywords: 


  These might be "not previously sold" as opposed to "new" at that
price. I just got a couple of Maxtor 320MB ESDI drives like that. No
problem with them, they have a warantee, but they say something like
RECERTIFIED on the cases.


To: 386users@crdos1
Cc: 
Subject: 



Our department had an Equity 386/20 donated to it a few weeks ago.
It currently is configured with the requisite minimum 1 Mbyte of RAM.
I have called a local Epson dealer, who tells me:
	 1) the Epson SIMM's are proprietary, and 
	 2) it will cost about $400 / Mbyte to purchase new RAM.

Is this guy just jerking me around or what?  Has anyone else heard this,
or better yet, does anyone have any contradictory evidence?  Any suggested
suppliers?

Thanks in advance,
dB


To: mayne@VSSERV.SCRI.FSU.EDU (William (Bill) Mayne)
Cc: 
Subject: Re: awk arguments
Reply-To: davidsen@crdos1.crd.ge.com (bill davidsen)


In article <290@sun13.scri.fsu.edu>, mayne@VSSERV.SCRI.FSU.EDU
	(William (Bill) Mayne) writes:

|> I have never been able to get the command line assignment of 
|> awk variables to work. I have sometimes resorted to ugly kludges 
|> like using sed to modify an awk program to hard code argument 
|> values. Surely I have missed something obvious. There must be a 
|> better way. I would be most grateful if someone would suggest one.

  In hopes that this will help others, I'll post instead of mailing...

  When you use a filename with =, all the rules are implementation
dependent. The BSD version of awk seems to take anything with an = as a
variable assign, and allows them in line:
	awk -f cmds a=1 file1 a=2 file2
which also works with most other variants. nawk and later SysV versions
allow interspersing of filenames and variable settings, which are
evaluated left to right, but if an arg has a / in it, it is always
treated as a filename. BSD awk (at least on Sun) doesn't seem to do
this, not does gawk, at least the version I have.

  Hope this helps, the bproblem is a bit tricky, and I just happened to
have access to most of the common versions of awk here.
--
Bill Davidsen (davidsen@crdos1.crd.ge.com, uunet!crdgw1!crdos1!davidsen)
  GE Corp R&D Center, Schenectady NY
  Moderator of comp.binaries.ibm.pc and 386users mailing list
"Stupidity, like virtue, is it's own reward" -me

roger@yuba.wrs.com (Roger Rohrbach) (07/25/90)

mayne@VSSERV.SCRI.FSU.EDU (William (Bill) Mayne) writes:

> I have never been able to get the command line assignment of 
> awk variables to work.

    I tried this on SunOS, too.  It seems like a bug.


> The practical problem this raises is how to communicate an
> argument value or a value calculated by a script file into
> an awk program embedded in a script file.

    This is simply a matter of escaping from the quoted awk program
to collect the values of shell variables.  It can be done anywhere in
your awk program, but I tend to assign them all in the BEGIN section:

    awk '

	BEGIN {
	    user = "'$USER'";
	    arg = "'$1'";
	}

	{
	    print user, arg
	}'

will set the awk variables user and arg to the values of the shell
variables $USER and $1, respectively, and then print them for each
awk input record.


> the version actually available on every flavor of unix
> I have seen is much weaker than the full version described by the book.

    Yes, the awk supplied with most Unix systems dates from UNIX Version 7.
The book describes a version you must buy from the AT&T Toolchest!  At
least, you had to do so when the book first came out; now you can get an
implementation of that language free- GNU awk (gawk).  It chokes on my
best work (written in "old awk"), however, so I don't bother with it.

    I am an old awk hand, and prefer the older ("weaker"?) version.  The
addition of functions and all manner of bells and whistles to my beloved
pattern-directed little language disappointed me.  To see how "weak" the
original awk is, see my recent posting of a complete LISP interpreter,
written in that version of awk, to comp.lang.lisp.

Roger Rohrbach                                  sun!wrs!roger    roger@wrs.com
- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
|   {o >o                                                                     |
|    \ -) "I was an infinitely hot and dense dot."                            |

briand@trigraph.uucp (Brian Dickson) (07/25/90)

In article <290@sun13.scri.fsu.edu> mayne@VSSERV.SCRI.FSU.EDU (William (Bill) Mayne) writes:
>Since unix file names may contain an = this is clearly ambiguous.
>
>[...]
>
>The practical problem this raises is how to communicate an
>argument value or a value calculated by a script file into
>an awk program embedded in a script file. Referencing $n in
>the quoted awk program won't work, since awk will think that
>refers to field number n.
>
>... Surely I have missed something obvious. There must be a 
>better way. I would be most grateful if someone would suggest one.

The problem is, ''-quotes hide special characters, which is why they
are often used. ""-quotes don't, thus letting shell substitutions occur.
But, you say, I want to use the $ in my awk script; how do I hide it from
the shell? The \ character inside "" will hide \,',", and $.
(Paraphrased from sh(1) on BSD4.3)

So, for example, the following script will take $1 as an assignment, $2
as a file, and run an awk script making use of the various combinations of
references to shell and awk variables.

	eval $1
	awk "{print \$0 \" $X $1 $2\"}" <$2

Call the file X=Y, and make it executable; see what "X=Y X=Y X=Y" does.

>
>... Are there better versions out there?
At our site we use nawk, which I believe is public domain. Consult an archive
site near you for more info.

--
Brian Dickson,
Trigraph Inc.
Disclaimer: Disclaimer is a trademark of AT&T.

vlb@magic.aux.apple.com (Vicki Brown) (07/26/90)

From miscellaneous postings Re: awk:

mayne@VSSERV.SCRI.FSU.EDU (William (Bill) Mayne) writes:

>> While I am on the subject of awk: I learned about the language
>> from the book "The AWK Programming Language" by Kernighan et. al.
>> before I started working on unix. I have been disappointed to
>> find that the version actually available on every flavor of unix
>> I have seen is much weaker than the full version described by the

The book is published by Addison-Wesley, 1988.  It is useful for the old
(not the new improved) awk, but covers the updated version because the
authors of the book are the authors of awk.  The book is very good.  If
you use awk, get it.  If you can't believe the new features are useful and
you don't have the book, GET IT.

>> book. Are there better versions out there?

briand@trigraph.uucp (Brian Dickson) writes:
>>> At our site we use nawk, which I believe is public domain. Consult an
>>> archive site near you for more info.

The new version of awk (often called nawk) has been extant since 1985.
"They" (AT&T) did manage to get it into Sys V.3.? and it will be in Sys V.4.
It is NOT public domain; however It is available from the AT&T Toolchest,
in source, for the nominal price of ~$300.  If that bothers you,
the Free Software Foundation (gnu) has a copyleft version called gawk which
they want to be fully compatible.

roger@yuba.wrs.com (Roger Rohrbach) writes:
|> implementation of that language free- GNU awk (gawk).  It chokes on my
|> best work (written in "old awk"), however, so I don't bother with it.

Roger - please send information about such failures to the gnu project.
Their aim is strong, good programs that work.  If gawk chokes on something
that awk handles, let gnu know so they can make things better!

omerzu@quando.quantum.de (Thomas Omerzu) writes:
>I've always been quite satisfied with the
>abilities of 'awk'
>
>- except of one point: the error checking of program scripts.
>You'll nearly ever get 'bailing out near line ...'.
>That point would have been worth of being mentioned in the
>'bug' section of the manual for awk.

Then you'll love the new version! One of the great new features is
CONTEXTUAL error messages:
code:
		BEGIN {
        		next = 7
        		exit
		}
error:
	awk: syntax error at source line 3
 	context is
                	next >>>  = <<<  7
	awk: illegal statement at source line 3

In old awk I get a syntax error and a segmentation fault.

Personally, I think new awk is great (functions, better substitution
capabilities, the cos function works!, etc.).  We've got it in-house
here and I use it all the time.  None of my old scripts have broken yet.

Since AT&T is finally recognizing it, I'd say expect to see it in more
versions of U*X coming along.

By the way, rumor has it that Brian Kernighan says "The name is awk, not
nawk", for which he gets even more of my respect that he already had.
-- vicki

gwyn@smoke.BRL.MIL (Doug Gwyn) (07/27/90)

In article <9413@goofy.Apple.COM> vlb@apple.COM (Vicki Brown) writes:
>By the way, rumor has it that Brian Kernighan says "The name is awk, not
>nawk", for which he gets even more of my respect that he already had.

The way I understand the naming is that it follows the same convention
that has been in use on UNIX systems since the beginning, whenever it
is thought that a new release of a utility might be incompatible with
the previous version:
	(1) Install the new version of "foo" as "nfoo";
	    link "foo" to "ofoo".
	(2) Users now have time to investigate what changes if any
	    their applications need to work with "nfoo".  If they
	    don't have time to deal with it, they should change to
	    using "ofoo" instead of "foo".  If something is newly
	    developed that depends on features present only in the
	    new version, it should invoke "nfoo".
	(3) Considerably later, "nfoo" is linked to "foo", replacing
	    the previous "foo" link.  "ofoo" remains installed.  Note
	    that all applications continue to work at this point.
	(4) Everyone is encouraged to finish converting from "ofoo"
	    to the new version of "foo", and to just use the name
	    "foo" instead of "nfoo".
	(5) Considerably later, "ofoo" and "nfoo" are removed/

QQ11@LIVERPOOL.AC.UK (07/27/90)

In article <1650@quando.quantum.de>, omerzu@quando.quantum.de (Thomas Omerzu)
says:
>
....
>
>| While I am on the subject of awk: I learned about the language
>| from the book "The AWK Programming Language" by Kernighan et. al.
>| before I started working on unix. I have been disappointed to
>| find that the version actually available on every flavor of unix
>| I have seen is much weaker than the full version described by the
>| book. Are there better versions out there?
>
>Well, I don't know that book
>and thus I don't know what weaknesses you're pointing at.
>Maybe you could tell us something more specific?
>
>I've always been quite satisfied with the
>abilities of 'awk'
>
Try paying AT&T some money for a better version or better still try
the GNU version GAWK which is "fully upward compatible with the System V
Release 3.1 and later version of awk" (GAWK manual).

Alan Thew
University of Liverpool Computer Laboratory
Bitnet/Earn: QQ11@LIVERPOOL.AC.UK or QQ11%UK.AC.LIVERPOOL @ UKACRL
UUCP       : ....!mcsun!ukc!liv!qq11        Voice: +44 51 794 3735
Internet   : QQ11@LIVERPOOL.AC.UK or QQ11%LIVERPOOL.AC.UK @ NSFNET-RELAY.AC.UK

martin@mwtech.UUCP (Martin Weitzel) (07/29/90)

In article <13424@smoke.BRL.MIL> gwyn@smoke.BRL.MIL (Doug Gwyn) writes:
[....]
:	(1) Install the new version of "foo" as "nfoo";
:	    link "foo" to "ofoo".
:	(2) Users now have time to investigate what changes if any
:	    their applications need to work with "nfoo".  If they
:	    don't have time to deal with it, they should change to
:	    using "ofoo" instead of "foo".  If something is newly
:	    developed that depends on features present only in the
:	    new version, it should invoke "nfoo".
:	(3) Considerably later, "nfoo" is linked to "foo", replacing
:	    the previous "foo" link.  "ofoo" remains installed.  Note
:	    that all applications continue to work at this point.
:	(4) Everyone is encouraged to finish converting from "ofoo"
:	    to the new version of "foo", and to just use the name
:	    "foo" instead of "nfoo".
:	(5) Considerably later, "ofoo" and "nfoo" are removed/

Does anybody know a way how we could print this in VERY LARGE LETTERS
and make sure that a copy is pinned to the wall near every terminal,
where someone is working on `improvements' of unix tools or commands?
-- 
Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83

johnny@edvvie.at (Johann Schweigl/104857600) (08/04/90)

From article <1650@quando.quantum.de>, by omerzu@quando.quantum.de (Thomas Omerzu):
> Well, I don't know that book
> and thus I don't know what weaknesses you're pointing at.
> Maybe you could tell us something more specific?

Aho's book covers nawk (new awk) which has some useful features the old awk
is missing (like multiple output files). 
A lot of current UNIX ports still include the old awk. You could try to get
gawk (GNU's awk version), which is nawk-compatible (at least I do believe so).
-- 
This does not reflect the   | Johann  Schweigl | DOS?
opinions of my employer.    | johnny@edvvie.at | Kind of complicated
I am busy enough by talking |                  | bootstrap loader ...
about my own ...            |   EDVG  Vienna   |