Claude.P.Cantin@NRC.CA (06/10/90)
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)ciemo@bananapc.wpd.sgi.com (Dave Ciemiewicz) (06/11/90)
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
ortwein@BRL.MIL (Mick Ortwein) (06/12/90)
Last I remember (when awk was my life), in order to bring
a shell variable into awk you need to use single quotes around
the variable. The effect is that the value of the variable is
written on top of the quoted word before awk interprets the rest
of the commands (sort of a preprocessor if you will).
Put the following two lines into a file called "example"
#!/bin/sh
awk '{printf("%s\n","'$HOME'")}' <example
When you execute this shell script the output should be:
/other/ortwein
/other/ortwein
(or whatever the HOME path is for you...)
-Mick (C is my life) Ortweinshoshana@pdi.UUCP (Shoshana Abrass) (06/12/90)
> 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