Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 17 May 2001 20:06:23 -0500
From:      Mike Meyer <mwm@mired.org>
To:        Lucas Bergman <lucas@slb.to>
Cc:        questions@freebsd.org
Subject:   Re: Help w/ Awk
Message-ID:  <15108.30095.521591.799161@guru.mired.org>
In-Reply-To: <108340844@toto.iv>

next in thread | previous in thread | raw e-mail | index | archive | help
Lucas Bergman <lucas@slb.to> types:
> > That's a simpler way of doing it, but doesn't accomplish what I'm trying to
> > do. I'm trying to be able to loop through each user and get it's UID/GID and
> > do stuff...
> > 
> > My problem wasn't with my AWK statement, but with my for/next loop.
> > 
> > For whatever reason (still unknown), the for Line in Password was failing,
> > and was returning multiple lines, like there were EOL's embedded in the
> > lines.
> 
> You're right.  The shell assumes the argument to 'for' is a list, a
> string that it breaks into list elements at _any_ whitespace, not just
> newlines.  So, if your /etc/passwd lines had whitespace in them (and
> they frequently do in the GECOS field), they would get broken in the
> middle.
> 
> A previous response already pointed out a solution, passing the file
> to awk, which iterates over whole lines by default.  You can use the
> shell's 'read' also:
> 
>   while read line; do
>     # operate on "$line"
>   done </etc/passwd
> 
> See sh(1).

You can combine this with pipes to get AWK to parse your password line
for you as well:

awk -F: '{ print $1 " " $3 " " $4 } ' /etc/passwd |
while read line
do
    set -- $line
    # Operate with $1, $2 and $3 set to 1st, 3rd and 4th fields from passwd
done

	<mike
--
Mike Meyer <mwm@mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?15108.30095.521591.799161>