Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 16 May 2001 08:58:36 -0700
From:      "C Peter Biessener" <pbiessener@hirshfields.com>
To:        "FreeBSD Questions" <freebsd-questions@freebsd.org>
Subject:   RE: Help w/ Awk
Message-ID:  <NEBBLEMLMLOBKEKOJLBHIECDCAAA.pbiessener@hirshfields.com>
In-Reply-To: <MOBBIPGJKBNNPGLGMFHFIEIBHCAA.don@whtech.com>

next in thread | previous in thread | raw e-mail | index | archive | help
I think your problem lies with the way for-do-done parses the input it is
given.  If you are comfortable with awk, I would write the entire script in
awk - using the associative arrays that awk has, then in the END {} section
print your output.  I've never personally read the awk manpage, I use the
O'Reilly book 'sed & awk' for a reference.

e.g.

$ awk -f _script.awk_ /etc/passwd > passwd.out

with _script.awk_ something like this:

BEGIN {
  FS=":"
  count = 1
}
{ user[count] = print $1
  UID[count]  = print $3
  GID[count]  = print $4
  count += 1
}
END {
  #print output here
}

-----Original Message-----
From: owner-freebsd-questions@FreeBSD.ORG
[mailto:owner-freebsd-questions@FreeBSD.ORG]On Behalf Of Don O'Neil
Sent: Tuesday, May 15, 2001 6:10 PM
To: freebsd-questions@FreeBSD.ORG
Subject: Help w/ Awk


I'm trying to write a simple script to extract the user name, UID and GID of
each user in the /etc/passwd file and I'm not quite sure what I'm doing
wrong here.... here's a code snippet;

#!/bin/sh
passwd=`cat /etc/passwd`

for user in $passwd

do

username=`echo $user | awk 'BEGIN { FS=":" } END { print $1 }'`

echo $username

done


Where am I going wrong with awk? Sometime the result of $username is the
user name, but sometimes its another part of the entry in the file. I just
want the user name stuck into username.

Thanks!


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


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?NEBBLEMLMLOBKEKOJLBHIECDCAAA.pbiessener>