Passing command line variables to AWK in shell script 
Author Message
 Passing command line variables to AWK in shell script

Hi All,

I (newbie to AWK) am trying to test a variable keyed into from the command
line for it's existence in another file. For example, user executes:

./testuser fooman

testuser script contains
#!/bin/bash
_KEYED=$1    # assigns variable _KEYED to whatever user keys into command
line.
_USER=`awk -F: '$1=="$_KEYED" {print $1}' /etc/passwd `
{comparison script stuff goes here...}

_USER variable never gets assigned because I think the shell script thinks
$1 is the command line variable while my AWK command thinks $1 is the input
from /etc/passwd.

How can I get awk to compare $1 (input field from /etc/passwd) to $_KEYED
(keyed-in command line variable)

Thanks in Advance!
Scott Glawson



Fri, 08 Jul 2005 02:31:37 GMT  
 Passing command line variables to AWK in shell script

Quote:
> Hi All,

> I (newbie to AWK) am trying to test a variable keyed into from the
command
> line for it's existence in another file. For example, user executes:

> ./testuser fooman

> testuser script contains
> #!/bin/bash
> _KEYED=$1    # assigns variable _KEYED to whatever user keys into
command
> line.
> _USER=`awk -F: '$1=="$_KEYED" {print $1}' /etc/passwd `
> {comparison script stuff goes here...}

> _USER variable never gets assigned because I think the shell script
thinks
> $1 is the command line variable while my AWK command thinks $1 is the
input
> from /etc/passwd.

> How can I get awk to compare $1 (input field from /etc/passwd) to
$_KEYED
> (keyed-in command line variable)

> Thanks in Advance!
> Scott Glawson

One way:

_USER=`awk -F: '$1=='"$_KEYED"' {print $1}' /etc/passwd `

another:
_USER=`awk -F: '$1==awkvar {print $1}' awkvar="$_KEYED" /etc/passwd `

HTH
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"



Fri, 08 Jul 2005 05:35:39 GMT  
 Passing command line variables to AWK in shell script


X Hi All,
X
X I (newbie to AWK) am trying to test a variable keyed into from the command
X line for it's existence in another file. For example, user executes:
X
X ./testuser fooman
X
X testuser script contains
X #!/bin/bash
X _KEYED=$1    # assigns variable _KEYED to whatever user keys into command
X line.
X _USER=`awk -F: '$1=="$_KEYED" {print $1}' /etc/passwd `
X {comparison script stuff goes here...}
X
X _USER variable never gets assigned because I think the shell script thinks
X $1 is the command line variable while my AWK command thinks $1 is the input
X from /etc/passwd.
X
X How can I get awk to compare $1 (input field from /etc/passwd) to $_KEYED
X (keyed-in command line variable)
X
X Thanks in Advance!
X Scott Glawson

I'm partial to using the -v option

        _USER=`awk -F: -v your_variable="$_KEYED" '
               $1 ~ your_variable { action ... }
        ' /etc/passwd`

You may specify as many -v options as you like.

Another option if you have too many -v options to pass, is to use
environment variables:

        export FIRST="abc"
        export SECOND="def"
        export THIRD="ghi"
        etc...

        awk '
                $1 == ENVIRON["FIRST"] { action ... }
                $2 == ENVIRON["SECOND"] { action ... }
                $3 == ENVIRON["THIRD"] { action ... }
        ' some_file

The environment variable names can be anything you want.  I just choose
FIRST, SECOND, THRID because I'm not being very imginative this evening
:-)

                                        Bob Harris



Fri, 08 Jul 2005 10:29:37 GMT  
 Passing command line variables to AWK in shell script

[in a shell script]

% _USER=`awk -F: '$1=="$_KEYED" {print $1}' /etc/passwd `

% _USER variable never gets assigned because I think the shell script thinks
% $1 is the command line variable while my AWK command thinks $1 is the input
% from /etc/passwd.

No. Your problem is that $_KEYED does not get expanded, since it appears
within single quotes. Try something like this:

 _USER=`awk -F: -v keyed="$_KEYED" '$1==keyed {print $1}' /etc/passwd `

which assigns an awk variable with the value of $_KEYED, then uses the
variable in the comparison.
--

Patrick TJ McPhee
East York  Canada



Fri, 08 Jul 2005 14:06:27 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Help passing command line parameters to awk scripts

2. THANKS: Help passing command line parameters to awk script

3. How to pass shell variable to awk?

4. pass a variable from awk to shell

5. passing shell variable into awk program

6. how do i pass shell variables into awk?

7. passing shell variable containing many fields to awk

8. Passing Shell Variables to Awk

9. Passing variable to shell in Awk...

10. shell commands within awk script

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

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

 

 
Powered by phpBB® Forum Software