Path: utzoo!attcan!uunet!aplcen!samsung!usc!apple!mips!sgi!shinobu!odin!bananapc.wpd.sgi.com!ciemo From: ciemo@bananapc.wpd.sgi.com (Dave Ciemiewicz) Newsgroups: comp.sys.sgi Subject: Re: awk question... Message-ID: <8792@odin.corp.sgi.com> Date: 11 Jun 90 16:32:07 GMT References: <9006091601.aa04003@VGR.BRL.MIL> Sender: news@odin.corp.sgi.com Reply-To: ciemo@bananapc.wpd.sgi.com (Dave Ciemiewicz) Organization: Silicon Graphics, Inc. Lines: 44 In article <9006091601.aa04003@VGR.BRL.MIL>, Claude.P.Cantin@NRC.CA writes: > > 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). > > If the userid is 123, the following would do just fine: > > awk -F: '$3 == 123 {print $1}' /etc/passwd > > BUT 123 is the content of a variable, say UID. The following does NOT > work: > > awk -F: '$3 == $UID {print $1}' /etc/passwd > > (the output is NOTHING). > > I have tried several variations, including "$UID", and "$3"=="$UID", etc., > but none worked... > > Anyone has an insight???? > > Thank you, > > Claude Cantin (CANTIN@VM.NRC.CA) Claude, Your problem is that shell variables are not expanded within single quotes ('). Try the following: awk -F: '$3 == '$UID' {print $1}' /etc/passwd You can also use: awk -F: '$3 == uid {print $1}' uid=$UID /etc/passwd You might check out the AWK book by Aho, Kernighan, and Weinberger from Addison-Wesley for more fun with AWK. --- Ciemo