I need to compare two Linux group files with Python and find a missing user in the group. I used the below code, but it failed if users are in a different order.
with open('group1', 'r') as file1:
with open('group2', 'r') as file2:
same = set(file1).difference(file2)
same.discard('\n')
with open('some_output_file.txt', 'w') as file_out:
for line in same:
file_out.write(line)
For example,
group1:
test:x:1234:mike,john,scott
test2:x:1234:mike,john
test3:x:1234:tim,dustin,Alex
group2:
test:x:1234:mike,scott,john
test2:x:1234:mike,john,scott
test3:x:1234:dustin,tim
the ideal output would be,
missing group1:
test2:scott
missing group2:
test3:Alex
Should I take each user and compare it? What would be the best way to compare two files?