sh & awk: Quoting Awk Syntax in Bourne shell? 
Author Message
 sh & awk: Quoting Awk Syntax in Bourne shell?

Hi

I want to get the user's home directory from YP passwd.

  $ new_user=userfoo
  $ ypcat passwd | awk -F: "\$1 == \"$new_user\" { print \$6 }"
  /home/userfoo

And I want to assign it to a shell variable, but awk returns an syntax
error.

  $ new_user_home=`ypcat passwd | awk -F: "\$1 == \"$new_user\" { print
\$6 }"`
  awk: syntax error near line 1
  awk: bailing out near line 1

The point that I don't quite understand is how the pair of backticks
change the quoting in Bourne sh such that it becomes invalid syntax for
awk, since the command within the backticks works on the command-line
without the backticks.  What have the pair of backticks done?

BTW, with trial-and-error, I found a better quoting that works.

  $ new_user_home=`ypcat passwd | awk -F: '$1 == "'$new_user'" { print
$6 }'`
  $ echo $new_user_home
  /home/userfoo

But, I'm still curious why the previous example fails.  FYI, awk is from
/usr/bin/awk on Solaris 2.5.1, 2.6 & 2.7.

Thanks.  I appreciate your enlightenment.

Regards
Hon-Chi

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 08 Nov 2002 03:00:00 GMT  
 sh & awk: Quoting Awk Syntax in Bourne shell?

Quote:

>   $ new_user=userfoo
>   $ ypcat passwd | awk -F: "\$1 == \"$new_user\" { print \$6 }"
>   /home/userfoo

Let ypmatch(1) do the work for you.

Quote:
> The point that I don't quite understand is how the pair of backticks
> change the quoting in Bourne sh such that it becomes invalid syntax for
> awk, since the command within the backticks works on the command-line
> without the backticks.  What have the pair of backticks done?

use echo instead of awk to find out what awk is being fed.

Regards,
--
Michael Sternberg,  Dipl. Phys.          | Uni-GH Paderborn
http://www.phys.uni-paderborn.de/~stern/ | FB6 Theoretische Physik
phone: +49-(0)5251-60-2329   fax: -3435  | 33098 Paderborn, Germany
"Who disturrrbs me at this time?"  << Zaphod Beeblebrox IV >>     <*>



Fri, 08 Nov 2002 03:00:00 GMT  
 sh & awk: Quoting Awk Syntax in Bourne shell?

Quote:

>  $ new_user_home=`ypcat passwd | awk -F: "\$1 == \"$new_user\" { print \$6 }"`
>The point that I don't quite understand is how the pair of backticks
>change the quoting in Bourne sh such that it becomes invalid syntax for
>awk, since the command within the backticks works on the command-line
>without the backticks.  What have the pair of backticks done?

Introduced another round of interpolations --- ``s interpolate
backslashes like ""s do; then when the command is run the $s,
now lacking their protective \s, get interpolated in the "s.

Quote:
>BTW, with trial-and-error, I found a better quoting that works.
>  $ new_user_home=`ypcat passwd | awk -F: '$1 == "'$new_user'" { print $6 }'`

Now there are no \s for the `s to strip, and the resulting
command has properly protected $s.

                --Ken Pizzini



Sat, 09 Nov 2002 03:00:00 GMT  
 sh & awk: Quoting Awk Syntax in Bourne shell?

Quote:

> Hi

> I want to get the user's home directory from YP passwd.

>   $ new_user=userfoo
>   $ ypcat passwd | awk -F: "\$1 == \"$new_user\" { print \$6 }"
>   /home/userfoo

You can simplify the quoting a little if you chose another way
to get the contents of the "new_user" shell script variable into
awk, i.e.

        $ new_user=userfoo
        $ ypcat passwd | awk -F: '$1 == new_user {print $6}' new_user=$new_user

This example uses single quotes for the awk script (simplifying
the quoting rules). The last part

        new_user=$new_user

sets the *awk* variable "new_user" with the contents of the *shell* variable
"new_user"

Heiner



Sat, 09 Nov 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. awk script in sh shell

2. Substituting single quotes from Perl via shell using awk and gsub

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

4. AWK & Shell Training Classes in Seattle

5. c-shell / bourne-shell / tclsh

6. Awk, SH, Perl -- Worth learning?

7. converting awk/sh to python

8. awk,sh and python

9. awk not printing single quotes

10. awk: putting quotes around a field value

11. printing single quote (apostrophe) with awk

12. Getting awk (or sed or anything else) to put single quotes into file

 

 
Powered by phpBB® Forum Software