Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!pdi.UUCP!shoshana From: shoshana@pdi.UUCP (Shoshana Abrass) Newsgroups: comp.sys.sgi Subject: Re: awk question... Message-ID: <9006111926.AA7340126@pdi.UUCP> Date: 11 Jun 90 19:26:43 GMT Sender: usenet@ucbvax.BERKELEY.EDU Organization: The Internet Lines: 31 > 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@sgi.sgi.com