Linux – Compare two user lists in bash

bashlinuxpasswd

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.

Best Answer

This should work for you using process substitution with bash, diff, awk, and sort:

diff <(awk -F: '{print $1}' /etc/passwd | sort) <(sort your_other_list_file)

This assumes your your_other_list_file only contains usernames, one per line. Can't help you parse that unless you post an example line.