I have two different user lists. I need to compare the users and make sure they exist in both files. One is the passwd file, and another is a flat file for that has the usernames and other information that I can extract a sorted username list from.

This gives me a sorted list of the usernames:

cat /etc/passwd | cut --fields=1 | sort -k1.2

Is there a better way to do this number one, and number two how do I then compare it the other user list from the other file? If the user does not exist I will be adding it to the flat file.

That appends lines that appear in file one (-v 1) that don't appear in file two to the end of file two. The fields for matching are field 1 for file one and field 3 for file two. The sed command compensates for the password file being colon-delimited and the flat file being space delimited. You can adjust field numbers and delimiters to suit your needs.

I like this best, it's a one-liner, avoids awk, and comm gives you options to give you entries unique to list1, list2, or commonalities to both. Also cutting before sorting gives sort shorter strings to work with so it might be faster on longer lists.

I changed my question around a bit, it's not really two comparable files.
–
spectoAug 6 '10 at 11:50

1

I can tell you what not do to. I was trying something similar to (command alerted slightly so that no one copies it without thinking): for i in cat /etc/passwd | cut -d' :' -f1; do if [ "grep $i /etc/passwd3" ]; then echo $i; fi; done It didn't do what I was expecting (list the usernames in both), instead it executed each username as a command. All well and good until it gets to the username "halt". There goes my uptime.
–
James LAug 6 '10 at 11:55

I need to re-create the user in the flat file with the extra parameters as well.
–
spectoAug 6 '10 at 12:24

You're not being specific enough, you tell us that they are not the same format and not comparable, but expect us to be able to determine which information needs to get into the mystery file.
–
James LAug 6 '10 at 12:26