Hey, I'm running an HP-UX box, trying to create a little script that will compare the /etc/passwd file with another file I have created.

Each line in this file, let's call it /etc/myusers, has a pin that the user has chosen (not necessarily unique), then their userid, then the comments field from /etc/passwd. (basically their full name, phone number, all that jazz.

Here is an example:

pin:userid:comments:
1234:pittb:"Brad Pitt,Los Angeles CA,123-123-1234":
9876:clooneyg:"George Clooney,Los Angeles CA,123-123-1234":
6548:dansont:"Ted Danson,Los Angeles CA,123-123-1234":
9685:owenc:"Clive Owen,Los Angeles CA,123-123-1234":
1652:simpsonh:"Homer Simpson,Los Angeles CA,123-123-1234":

I basically want to make sure that every account in my /etc/passwd file is also in this file. So all I want to do is go through each line in /etc/passwd, and search for each userid in this custom file. If the name DOESN'T exist, I want to print it out, to the screen, or a file, or whatever, so I can go through and add all the accounts to this file.

It should be fairly simple, I just can't quite figure it out.

I was thinking of just doing an awk on the /etc/passwd file and pulling out all the usernames to a file, and then doing a for loop through that file, or something like this:

root# awk -F: '{ print $1 }' /etc/passwd > users.txt

root# for x in `cat users.txt` ; do
>grep $x /etc/myusers
>if no result, print to file or whatever...?

Just not quite sure what.

Thanks for your help.

How about something like this:

Do the awk to get all the usernames out:

root# awk -F: '{ print $1 }' /etc/passwd > users.txt

Then do a for loop with a grep -v against all the usernames in /etc/passwd? Like this:

for x in `cat users.txt` ; do
> grep -v $x myusers
> done

Only problem with that is, it doesn't compile one nice list. It does it for every line in the users.txt file, thus printing out every line of the myusers file for every line in the /etc/password file. That's a mess. Any ideas with that?

Or what about sed? Couldn't I do a similar for loop with sed, and remove every line that matches $x? I have tried this to, but it's not working. Any ideas would help.

found something that works:

awk -F: 'FNR==NR { 
              users[$2]
              next
         } 
         !( $1 in users ) { print $0 }' myusers /etc/passwd
This article has been dead for over six months. Start a new discussion instead.