[comp.sys.sgi] awk vs echo

SOFPJF@VM.UOGUELPH.CA (Peter Jaspers-Fayer) (02/12/91)

I guess that even after a year of trying, I do not understand the way
the shell (csh) passes things to it's children, viz:

This is an excerpt from a longer awk command, and I'm stuck:

This works
           awk '{ print "'$user': " $0}'
as does
           awk '{ print "'`hostname`': " $0}'
But not
           awk '{ print "'`date`': " $0}'     (Spaces(?) Colons(?))
Even tho
          echo '{ print "'`date`': " $0}'     works OK, and if I CUT&PASTE
the result of the echo into another awk '', something like:

           awk '{ print "Mon Feb 11 11:35:32 EST 1991: " $0}'    IT WORKS!

Why are the last and 2nd-last awks different?  Anyone please?

 /PJ                                                SofPJF@VM.UoGuelph.Ca
         --------------------------------------
How did a fool and his money get together in the first place?       -Anon

jim@baroque.Stanford.EDU (James Helman) (02/12/91)

The problem is that date(1) prints out a space separated list which
breaks the argument to awk.  If you eliminate the spaces, it works,
e.g.:

	awk '{ print "'`date | sed -e s/\ /:/g`': " $0}' 

Jim Helman
Department of Applied Physics			Durand 012
Stanford University				FAX: (415) 725-3377
(jim@KAOS.stanford.edu) 			Work: (415) 723-9127

dprclf@ARCO.COM ("Chris L. Fouts") (02/12/91)

In message <9102111145.aa01900@VGR.BRL.MIL>; "Peter Jaspers-Fayer" writes:
> I guess that even after a year of trying, I do not understand the way
> the shell (csh) passes things to it's children, viz:
> 
> This is an excerpt from a longer awk command, and I'm stuck:
> 
> This works
>            awk '{ print "'$user': " $0}'
> as does
>            awk '{ print "'`hostname`': " $0}'
> But not
>            awk '{ print "'`date`': " $0}'     (Spaces(?) Colons(?))
> Even tho
>           echo '{ print "'`date`': " $0}'     works OK, and if I CUT&PASTE
> the result of the echo into another awk '', something like:
> 
>            awk '{ print "Mon Feb 11 11:35:32 EST 1991: " $0}'    IT WORKS!
> 
> Why are the last and 2nd-last awks different?  Anyone please?


The first awk winds up as

	awk '{ print "'Mon Feb 11 11:35:32 EST 1991': " $0}'

which has the awk program spread across multiple arguments as highlighted
below:

	awk '{ print "'Mon Feb 11 11:35:32 EST 1991': " $0}'
            ^^^^^^^^^^^^^^ ^^^ ^^ ^^^^^^^^ ^^^ ^^^^^^^^^^^^^  = 6 args

whereas

	awk '{ print "Mon Feb 11 11:35:32 EST 1991: " $0}'

has the awk program in only 1 argument.

To accomplish what you want (get ready for yet more quotes), try:

	awk '{ print "'"`date`"': " $0}'
                       ^      ^
		       |      |
		       +------+----These quotes prevent the spaces in the
				   date output from splitting up the
				   argument.


My head starts hurting if I look at this too long....

-- 

Chris L. Fouts
Email: dprclf@phobos
Ext: 3850

"Fate is the path of least action."
                             -- Kim Stanley Robinson in "A Short, Sharp Knock"

"Every day, I sit in traffic and consider my fate...."