Vars in an awk command/revisit 
Author Message
 Vars in an awk command/revisit

Runinning Linux OS
gawk-3.0.3-7

Charles Demas once showed me one way to put a shell var into awk
output.

Like so:
awk '/RE/{print var ,$1 ,$2}' var="`date +"%D %T"`" FILE

And this works without fail (thanks Charles)

Now I have need of putting this same kind of thing in an awk script.
Something like:

/RE/{print var, $1,$2}
var="`date +"%D %T"`"

But where ever I put it I get a message like this:

  awk: test.awk:3: (FILENAME=FILE FNR=1) fatal: division by zero
  attempted in mod

I've tried the above arrangement and every other I could think of.

BEGIN { var="`date +"%D %T"`"}
/RE/var="`date +"%D %T"`"{print var, $1,$2}

  awk: test.awk:3: fatal: division by zero attempted in mod

Tried various arrangements with the quotes including no quotes and
escaped quotes.

Tried:
/RE/{print var, $1,$2} var="`date +"%D %T"`"

  awk: test.awk:6: (FILENAME=.emacs FNR=1) fatal: division by zero
  attempted in mod

var=`date +"%D %T"`
/RE/{print var, $1,$2}

   awk: test.awk:6: var=`date +"%D %T"`
   awk: test.awk:6:     ^ Invalid char '`' in expression

So what is the trick to this?



Wed, 19 Jun 2002 03:00:00 GMT  
 Vars in an awk command/revisit


Quote:
>Runinning Linux OS
>gawk-3.0.3-7

>Charles Demas once showed me one way to put a shell var into awk
>output.

>Like so:
>awk '/RE/{print var ,$1 ,$2}' var="`date +"%D %T"`" FILE

>And this works without fail (thanks Charles)

>Now I have need of putting this same kind of thing in an awk script.
>Something like:

>/RE/{print var, $1,$2}
>var="`date +"%D %T"`"

>But where ever I put it I get a message like this:

>  awk: test.awk:3: (FILENAME=FILE FNR=1) fatal: division by zero
>  attempted in mod

>I've tried the above arrangement and every other I could think of.

>BEGIN { var="`date +"%D %T"`"}
>/RE/var="`date +"%D %T"`"{print var, $1,$2}

>  awk: test.awk:3: fatal: division by zero attempted in mod

>Tried various arrangements with the quotes including no quotes and
>escaped quotes.

>Tried:
>/RE/{print var, $1,$2} var="`date +"%D %T"`"

>  awk: test.awk:6: (FILENAME=.emacs FNR=1) fatal: division by zero
>  attempted in mod

>var=`date +"%D %T"`
>/RE/{print var, $1,$2}

>   awk: test.awk:6: var=`date +"%D %T"`
>   awk: test.awk:6:     ^ Invalid char '`' in expression

>So what is the trick to this?

You could do something like this:

#!/usr/local/bin/gawk  -f
BEGIN{"date +'%D %T'" | getline var}
{print $0, var}
# End of script

if that is temp.sh, then

echo abc | temp.sh

produces:

abc 01/01/00 18:12:50

Happy New Year, BTW.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Wed, 19 Jun 2002 03:00:00 GMT  
 Vars in an awk command/revisit

[...]

Quote:

> >So what is the trick to this?

> You could do something like this:

> #!/usr/local/bin/gawk  -f
> BEGIN{"date +'%D %T'" | getline var}
> {print $0, var}
> # End of script

> if that is temp.sh, then

> echo abc | temp.sh

> produces:

> abc 01/01/00 18:12:50

> Happy New Year, BTW.

Thanks,  boy those goose eggs look funny though don't they.


Wed, 19 Jun 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How do I get vars into awk?

2. #!/bin/awk -f style and shell vars

3. Shell vars into Awk executable script not so easy

4. passing vars to awk script

5. Passing and getting vars. from C to awk

6. initialize vars to zero in awk (long)

7. HELP: Passing Unix vars to AWK

8. command line vars and effectivity

9. Access commands list and vars list from C API

10. unknown vars in button-command

11. Accessing local vars in -command block...

 

 
Powered by phpBB® Forum Software