shell variable 
Author Message
 shell variable

How do I pass a shell variable to awk in the same shell script?

e.g.

#!/bin/ksh
group=$1
fn=$2

cat $fn  | awk '
BEGIN {
        awk code that needs to access group

Quote:
}'



Sat, 27 Mar 2004 23:48:04 GMT  
 shell variable

Quote:
> #!/bin/ksh
> group=$1
> fn=$2

> cat $fn  | awk '
> BEGIN {
>    awk code that needs to access group
> }'

awk '
BEGIN {
  print environ["group"]
Quote:
}

'

--
BBP



Sun, 28 Mar 2004 00:57:24 GMT  
 shell variable

Quote:


> > #!/bin/ksh
> > group=$1
> > fn=$2

> > cat $fn | awk '
> > BEGIN {
> >       awk code that needs to access group
> > }'

> awk '
> BEGIN {
>   print environ["group"]
> }
> '

Let's test that:

$ cat test.ksh
#!/bin/ksh
group=$1
awk '
BEGIN {
    print environ["group"]
    print ENVIRON["group"]

Quote:
}

'
$ ./test.ksh foobar

$ uname

The shell variable 'group' is not, nor should it be, an environment
variable. It's a shell variable, plain 'n' simple. There are several
ways to pass a shell variable into an awk script, and this isn't one
of them.

Your answer is wrong on multiple levels.

--
Jim Monty

Tempe, Arizona USA



Sun, 28 Mar 2004 03:55:55 GMT  
 shell variable

Quote:

> How do I pass a shell variable to awk in the same shell script?
> e.g.

> #!/bin/ksh
> group=$1
> fn=$2

> cat $fn  | awk '
> BEGIN {
>    awk code that needs to access group
> }'

You *should* be able to simply use the -v option. (You won't be
able to use it if you're running a very old version of awk.)

Try this:

$ cat test.ksh
#!/bin/ksh
group="$1"
awk -v group="$group" 'BEGIN { print group }'
$ ./test.ksh foobar
foobar
$

If that doesn't work, then pass the variable as the first file name
argument on the awk command line and access it via the ARGV array
within the BEGIN rule.

$ cat test.ksh
#!/bin/ksh
group="$1"
awk 'BEGIN { print ARGV[1]; ARGV[1] = "" }' "$group" /dev/null
$ ./test.ksh foobar
foobar
$

If you don't need the value of the shell variable within a BEGIN
rule, then the best way to pass it into the awk script is via the
standard and well-documented var=val command argument mechanism:

$ cat test.ksh
#!/bin/ksh
group="$1"
echo bar | awk '{ print group $1 }' group="$group"
$ ./test.ksh foo
foobar
$

Don't let anybody talk you into employing needless chicanery with
shell quoting tricks or, worse, attempting to use the non-standard
ENVIRON array for a simple shell variable.

--
Jim Monty

Tempe, Arizona USA



Sun, 28 Mar 2004 04:51:05 GMT  
 shell variable

Quote:

>> How do I pass a shell variable to awk in the same shell script?
>> e.g.

>> #!/bin/ksh
>> group=$1
>> fn=$2

>> cat $fn  | awk '
>> BEGIN {
>>        awk code that needs to access group
>> }'

>You *should* be able to simply use the -v option. (You won't be
>able to use it if you're running a very old version of awk.)

[snip]

Quote:
>$

>Don't let anybody talk you into employing needless chicanery with
>shell quoting tricks or, worse, attempting to use the non-standard
>ENVIRON array for a simple shell variable.

In what sense do you mean non-standard here exactly? I thought
it was a standard feature of 'new' awk. Are you saying that
there are problems with it on some implementations ?

byefrom

--
: ${L:-aura} # http://lf.8k.com:80
:            # http://lf.1accesshost.com

- Show quoted text -

Quote:

>--
>Jim Monty

>Tempe, Arizona USA



Sat, 27 Mar 2004 20:25:23 GMT  
 shell variable

Quote:


> > Don't let anybody talk you into employing needless chicanery with
> > shell quoting tricks or, worse, attempting to use the non-standard
> > ENVIRON array for a simple shell variable.

> In what sense do you mean non-standard here exactly?

In the sense that it's not defined in _The AWK Programming Language_,
the book that defines the language ('new' awk).

Quote:
> I thought it was a standard feature of 'new' awk.

It's a standard feature of the awk defined by POSIX 1003.1, the
other book that defines the language. Differently.

See the POSIX COMPATIBILITY section of the gawk(1) man page and
the PORTABILITY section of the MKS awk(1) man page.

Quote:
> Are you saying that there are problems with it on some implementations?

No, I'm not saying that. And I'm unaware of any problems with it
in any implementation. I only tossed in the expression "non-standard"
as a rhetorical flourish because I thought the occasion deserved it.

When you want access to an environment variable from within an awk
script, the ENVIRON array is definitely the way to go! It's what I use.

If you want an incorrect answer to the question "How do I pass a
shell variable into an awk script?", there's no need to come here
looking for it. There's one in the FAQ.

--
Jim Monty

Tempe, Arizona USA



Sun, 28 Mar 2004 11:35:40 GMT  
 shell variable


[...]

% The shell variable 'group' is not, nor should it be, an environment
% variable. It's a shell variable, plain 'n' simple.

It should be an environment variable if the goal is to use the
environment to pass it to another application. It's not necessary
with awk, but there's nothing wrong with doing it that way.

--

Patrick TJ McPhee
East York  Canada



Sun, 28 Mar 2004 12:30:43 GMT  
 shell variable




% > > Don't let anybody talk you into employing needless chicanery with
% > > shell quoting tricks or, worse, attempting to use the non-standard
% > > ENVIRON array for a simple shell variable.
% >
% > In what sense do you mean non-standard here exactly?
%
% In the sense that it's not defined in _The AWK Programming Language_,
% the book that defines the language ('new' awk).

What if it's in a public errata to the book? What if it's in the
reference implementation? What if it's been in the reference
implementation longer than the -v option that you espouse? What if it's
part of the POSIX standard and part of the single unix specification
standard (which are standards in the sense that people met and yammered
and yammered, espousing their stupid viewpoints almost as much as you
do, until enough people were tired and the last one standing won)?

--

Patrick TJ McPhee
East York  Canada



Sun, 28 Mar 2004 12:48:37 GMT  
 shell variable


...

Quote:
>No, I'm not saying that. And I'm unaware of any problems with it
>in any implementation. I only tossed in the expression "non-standard"
>as a rhetorical flourish because I thought the occasion deserved it.

"rhetorical flourish" = nice way of saying "BS"

Quote:
>When you want access to an environment variable from within an awk
>script, the ENVIRON array is definitely the way to go! It's what I use.

>If you want an incorrect answer to the question "How do I pass a
>shell variable into an awk script?", there's no need to come here
>looking for it. There's one in the FAQ.

And if you want a c.l.a. poster who is full of himself, frequently OT and/or
wrong, look no further than Jim Monty.


Sun, 28 Mar 2004 19:49:40 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. awk variables --> shell variables

2. AWK-Script to set a shell variable (Win32) ?

3. Passing shell variables to nawk snipet

4. setting shell variables with results from awk

5. NF and shell variables

6. how to use a shell variable in awk for pattern matching

7. Shell variables in AWK

8. Quoting Swamp: Saving an awk command in a shell variable

9. print shell variable

10. How to pass shell variable to awk?

11. Getting awk computed value into shell variable

12. shell variables with slashes

 

 
Powered by phpBB® Forum Software