awk question...

Shoshana Abrass shoshana at pdi.UUCP
Tue Jun 12 05:26:43 AEST 1990


> I'm writting a script in which a variable takes the value of a
> userid.  I then want to find out who this userid refers to.
>
> I want to do that in one line, involving awk (I know how to do it
> using multiple lines of code).
>
> BUT 123 is the content of a variable, say UID.  The following does
> NOT work:
>
>     awk -F: '$3 == $UID {print $1}' /etc/passwd

  The problem is that you are trying to access a shell variable from
  within an awk script -- since awk has its own 'variable space', it
  thinks that UID isn't set. You can set awk variables on the command
  line, with this general syntax:

        awk 'commands' var=text filename

  Thus your example would become:

        awk -F: '$3 == AWK_UID {print $1}' AWK_UID=$UID /etc/passwd
                     ^^^     ^^^
  Notice the lack of both the $ sign and the double-quotes around
  AWK_UID. 

  Good luck! I know I found this in the awk book somewhere, but I
  can't find the page now or I'd refer you to it.....

  -shoshana
  Shoshana Abrass
  pdi!shoshana at sgi.sgi.com



More information about the Comp.sys.sgi mailing list