awk and shell question

Larry Taborek larry at macom1.UUCP
Tue Sep 26 00:11:15 AEST 1989


>From article <1163 at ispi.UUCP>, by jbayer at 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 at 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 at macom1.UUCP		11400 Commerce Park Drive
						Reston, VA 22091-1506
						703-758-7000



More information about the Comp.unix.wizards mailing list