using awk in a shell script... 
Author Message
 using awk in a shell script...

Hi,

so this command (from bash):

egrep 'suzanne' data.txt | awk '{print$3}'

does exactly what I want it to do, it searches for the line in "data.txt"
that starts with suzanne,
then prints out the 3rd argument in that line.

However, when I try to port this to a shell script as follows:

#!/bin/sh
FILE=data.txt
name=$1  #this is the user's name (the first arg passed when invoking the
script)
num=$2   #this is the game number (the second arg passed when invoking the
script)
egrep $name $FILE | awk '{print $2}'

the print statement following "awk" is causing the 2nd argument to ALWAYS be
printed
as opposed to printing the xth argument (where x is passed as the 2nd
argument to the
shell script).

Seems like since the $2 is enclosed in braces and particularly single
quotes, the program
is interpreting $2 as a static var as opposed to letting $2 be three in
which case the
third var would be printed.

Sorry to be sooooooo lengthy, any help?

Tia.
Liz



Wed, 20 Apr 2005 11:20:04 GMT  
 using awk in a shell script...

Quote:

>Hi,

>so this command (from bash):

>egrep 'suzanne' data.txt | awk '{print$3}'

>does exactly what I want it to do, it searches for the line in "data.txt"
>that starts with suzanne,
>then prints out the 3rd argument in that line.

#!/usr/bin/gawk
BEGIN   {
        for (i=1; i<=ARGC; i++) args[i] = ARGV[i]
        ARGV[1] = "data.txt"
        ARGC = 2
        }
$0 ~ args[1] { print $args[2] }


Wed, 20 Apr 2005 11:57:04 GMT  
 using awk in a shell script...

Quote:

> Hi,

> so this command (from bash):

> egrep 'suzanne' data.txt | awk '{print$3}'

> does exactly what I want it to do, it searches for the line in "data.txt"
> that starts with suzanne,
> then prints out the 3rd argument in that line.

> However, when I try to port this to a shell script as follows:

> #!/bin/sh
> FILE=data.txt
> name=$1  #this is the user's name (the first arg passed when invoking the
> script)
> num=$2   #this is the game number (the second arg passed when invoking the
> script)
> egrep $name $FILE | awk '{print $2}'

> the print statement following "awk" is causing the 2nd argument to ALWAYS be
> printed

    Because that's what $2 means in awk.

Quote:
> as opposed to printing the xth argument (where x is passed as the 2nd
> argument to the
> shell script).

    You are passing a literal $2, not the second argument, because it
    is enclosed in single quotation marks.

Quote:
> Seems like since the $2 is enclosed in braces and particularly single
> quotes, the program
> is interpreting $2 as a static var as opposed to letting $2 be three in
> which case the
> third var would be printed.

     You could put the awk script in double quotation marks, in which
     case it would replace $2 with the second argument and print a
     literal "3".

     You need to convert the shell variable, $2, into an awk variable
     (or constant). There are varous ways to do it:

         egrep $name $FILE | awk "{print \$$2}"

         egrep $name $FILE | awk -v n=$2 '{print $n}'

         egrep $name $FILE | awk '{print $n}' n=$2

      But you don't need both egrep (why egrep instead of grep?) and
      awk:

         awk "/$1/ {print \$$2}"

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License



Wed, 20 Apr 2005 12:31:47 GMT  
 using awk in a shell script...
Chris --

you're brilliant. thanks.

Could I ever trouble a follow-up question (to
you or to the studio audience) of  *your
favorite* UNIX & assorted tools book?

Like the equivalent of W. Richard Steven's TCP/IP
Illustrated books -- the creme de la creme. :)

PS: I was a bit surprised that *even though* I
defined n=$2 earlier in the code, I needed to
do it again after the awk statement -- but I guess
that makes sense now - $2 is defined as "n" w/in
the context of the awk portion of the shell script
as_well_as w/in the larger context of the *.sh file.

[i.e. I'd tried blah-blah | awk '{print $n}' where
n=$2 was a few lines up...]



Quote:

> > Hi,

> > so this command (from bash):

> > egrep 'suzanne' data.txt | awk '{print$3}'

> > does exactly what I want it to do, it searches for the line in
"data.txt"
> > that starts with suzanne,
> > then prints out the 3rd argument in that line.

> > However, when I try to port this to a shell script as follows:

> > #!/bin/sh
> > FILE=data.txt
> > name=$1  #this is the user's name (the first arg passed when invoking
the
> > script)
> > num=$2   #this is the game number (the second arg passed when invoking
the
> > script)
> > egrep $name $FILE | awk '{print $2}'

> > the print statement following "awk" is causing the 2nd argument to
ALWAYS be
> > printed

>     Because that's what $2 means in awk.

> > as opposed to printing the xth argument (where x is passed as the 2nd
> > argument to the
> > shell script).

>     You are passing a literal $2, not the second argument, because it
>     is enclosed in single quotation marks.

> > Seems like since the $2 is enclosed in braces and particularly single
> > quotes, the program
> > is interpreting $2 as a static var as opposed to letting $2 be three in
> > which case the
> > third var would be printed.

>      You could put the awk script in double quotation marks, in which
>      case it would replace $2 with the second argument and print a
>      literal "3".

>      You need to convert the shell variable, $2, into an awk variable
>      (or constant). There are varous ways to do it:

>          egrep $name $FILE | awk "{print \$$2}"

>          egrep $name $FILE | awk -v n=$2 '{print $n}'

>          egrep $name $FILE | awk '{print $n}' n=$2

>       But you don't need both egrep (why egrep instead of grep?) and
>       awk:

>          awk "/$1/ {print \$$2}"

> --
>     Chris F.A. Johnson                        http://cfaj.freeshell.org
>     ===================================================================
>     My code (if any) in this post is copyright 2002, Chris F.A. Johnson
>     and may be copied under the terms of the GNU General Public License



Thu, 21 Apr 2005 02:40:23 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Getting A Day Using AWK or A Shell Script

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

3. Passing command line variables to AWK in shell script

4. trying to call awk from within a shell script

5. A simple question regarding shell scripts and awk

6. executable awk scripts and shell var $(basename $0)

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

8. Add more functionality to shell/awk script

9. Shell vars into Awk executable script not so easy

10. How awk use varible in a shell script

11. shell commands within awk script

12. Problem with awk and shell script

 

 
Powered by phpBB® Forum Software