Make awk print the value of shell variable `$@' 
Author Message
 Make awk print the value of shell variable `$@'

awk     = gawk-3.0.4-2
shell   = bash-2.04-11

Wanting to write some awk code that collects and makes use of a
variable number of shell arguments.  My first stumbling block has been

have the shell args indexed by their last 2 characters in an awk array,
that can be used by awk to call out specific args.

May be a totally misguided use of awk arrays since I don't really
understand how to use them.  I'd like to give arguments to a shell
script and have awk tuck them away.  Then call and use them as needed.

An example might be `script foo-b bar-b baz-a boz-a FILES
Where the awk array would contain all the args, and those ending in
`-b' could be called for one purpose, those ending `-a' another.'

So using the simplest case:


statement?

Assuming a shell script `SCRIPT' that may be given an unknown number of
arguments .  Here I use 3 for an example

SCRIPT one two three

SCRIPT contains these lines:
#!/bin/sh

awk 'BEGIN {print '"$#"'}'

which produces the expected:
   one two three
   3

   3

But changing the awk statement to:
#!/bin/sh


   Fails with:
   one two three
   3

   awk: cmd. line:2: (END OF FILE)
   awk: cmd. line:2: parse error

Or in some other way get awk to `remember' all args and call them when needed.



Mon, 07 Apr 2003 03:00:00 GMT  
 Make awk print the value of shell variable `$@'

Quote:

> #!/bin/sh



I'd just put it in an 'awk' variable:


--
Michael Hall
http://www.enteract.com/~mghall



Mon, 07 Apr 2003 03:00:00 GMT  
 Make awk print the value of shell variable `$@'

Quote:

> Wanting to write some awk code that collects and makes use of a
> variable number of shell arguments. My first stumbling block has been


You're struggling successfully to make something simple hard. ;-)
Awk gives you ready access to its command-line arguments via the
built-in ARGV array. So passing the shell's command-line arguments
into an awk script is trivial. Here's a simple demonstration:

  $ cat script
  #!/bin/sh


  $ script foo-b bar-b baz-a boz-a FILES
  5 foo-b bar-b baz-a boz-a FILES
  1 foo-b
  2 bar-b
  3 baz-a
  4 boz-a
  5 FILES
  $

In another thread, I posted a citation to the script named "field"
on p. 66 of _The AWK Programming Language_. Here is that script
(from <http://cm.bell-labs.com/cm/cs/who/bwk/awkcode.txt>):

  # field - print named fields of each input line
  #   usage:  field n n n ... file file file ...

  awk '
  BEGIN {
      for (i = 1; ARGV[i] ~ /^[0-9]+$/; i++) { # collect numbers
          fld[++nf] = ARGV[i]
          ARGV[i] = ""
      }
      if (i >= ARGC)   # no file names so force stdin
          ARGV[ARGC++] = "-"
  }
  {   for (i = 1; i <= nf; i++)
          printf("%s%s", $fld[i], i < nf ? " " : "\n")
  }
  ' $*

Replace the regular expression pattern /^[0-9]+$/ with something
like /-[A-Za-z]$/ and you're rounding third base, my friend!

Quote:
> May be a totally misguided use of awk arrays since I don't really
> understand how to use them. I'd like to give arguments to a shell
> script and have awk tuck them away. Then call and use them as needed.

Read section 2.6 Interaction with Other Programs on pp. 64-66 of
_The AWK Programming Language_.

Hope this helps.

--
Jim Monty

Tempe, Arizona USA



Mon, 07 Apr 2003 03:00:00 GMT  
 Make awk print the value of shell variable `$@'

Quote:


> > Wanting to write some awk code that collects and makes use of a
> > variable number of shell arguments. My first stumbling block has been

> You're struggling successfully to make something simple hard. ;-)

Something I'm verg good at...

Quote:
> Awk gives you ready access to its command-line arguments via the
> built-in ARGV array. So passing the shell's command-line arguments

[...]

Your comments are detailed an helpful as usual,.. thanks

Digesting this information and its implications now

Quote:

> Read section 2.6 Interaction with Other Programs on pp. 64-66 of
> _The AWK Programming Language_.

> Hope this helps.

Looks like just what I need to study.


Mon, 07 Apr 2003 03:00:00 GMT  
 Make awk print the value of shell variable `$@'

Quote:


> > #!/bin/sh


> I'd just put it in an 'awk' variable:



Did you run that successfully before posting?  Doesn't work here:

cat args.sh
   #!/bin/sh


args.sh one two three
   one two three
   3

   awk: cmd. line:2: fatal: cannot open file `three' for reading (No
   such file or directory)



Mon, 07 Apr 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. help for newbie: way to get the value of an awk variable in a shell script

2. Getting awk computed value into shell variable

3. how to print a shell variable in awk states

4. accessing a shell variable in an awk print statement

5. awk variables --> shell variables

6. How to write a macro for printing variable's name and its value

7. @U$`')-`:023/7H

8. Return value from awk snipet via korn shell subroutine

9. giving awk values back to the shell

10. question about making persistant shell variables from a script

11. understanding 'property' / making value read-only

 

 
Powered by phpBB® Forum Software