Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!purdue!haven!grebyn!macom1!larry
From: larry@macom1.UUCP (Larry Taborek)
Newsgroups: comp.unix.wizards
Subject: Re: awk and shell question
Message-ID: <4926@macom1.UUCP>
Date: 25 Sep 89 14:11:15 GMT
References: <1163@ispi.UUCP>
Organization: CENTEL Federal Systems, Reston, VA. 22091-1506
Lines: 52

From article <1163@ispi.UUCP>, by jbayer@ispi.UUCP (Jonathan Bayer):
> 
> HELP!!  I have been pulling my hair out over this seemingly simple
> problem:
> 
> LOGNAME is set nicely here.  However, when I try to do something similar
> to /etc/passwd as follows:
> a=" awk -F: '\$1 == \"$LOGNAME\" {
> 	user=\$5;
> 	print user
> 	}'"
> USER=`cat /etc/passwd | $a`
> 
> I get the following error:
> 
> awk: syntax error at source line 1
>  context is
> 	 >>> ' <<< 
> awk: bailing out at source line 1
> Now, the following line does work (when I hardcode in LOGNAME):
> USER=`cat /etc/passwd | awk -F: '$1 == "root" { user=$5; print user }'`

> Jonathan Bayer		Intelligent Software Products, Inc.
> (201) 245-5922		500 Oakwood Ave.
> jbayer@ispi.COM		Roselle Park, NJ   07204    

Jonathan,

try this instead:

USER=`cat /etc/passwd | awk -F: '{printf("%-8.8s %-20.20s\n",$1,$5)}'`
echo $USER

this works, but I think what you really want is:

cat /etc/passwd | awk -F: '{printf("%-8.8s %-20.20s\n",$1,$5)}'

without the USER=`

the awk script basically prints the 1st and 5th fields in a printf
statement, where the contents of $1 are put in a field 8 characters
long left justified, and the contents of $5 are put into a field
20 characters long left justified.  The \n should be a line feed.

Hope this helps...

Larry
-- 
Larry Taborek	..!uunet!grebyn!macom1!larry	Centel Federal Systems
		larry@macom1.UUCP		11400 Commerce Park Drive
						Reston, VA 22091-1506
						703-758-7000