Many of my servers' /etc/group file have many userid's that does not exist in /etc/passwd file and they need to be deleted.
This happened due to manual manipulation of /etc/passwd files.
I need to do this for 40 servers.
Can anyone help me in achieving this? Even reducing a step or two will be greatly appreciated.
Thanks in advance.
Hope Perl is OK.
file1:
matt:1:2:3
kristen:1:2:3
curt:1:2:3
jennifer:1:2:3
file2:
jennifer:1:2:3
kristen:1:2:3
curt:1:2:3
Here's the code:
#!/usr/bin/perl
# names in file1 not in file2
foreach (@names=`cut -d ':' -f1 file1`) {
chomp(@names);
$count = `grep -c $_ file2`;
if ($count == 0) {
print "$_ is in file1 but not file2.\n";
}
}
$ ./cleanup.pl
matt is in file1 but not file2.
$
Of course there's probably a really simple way to do it...I'll leave that to much smarter people.
HTH
Another solution:
awk ' FNR==NR{for (h=4;h<=NF;h++)a[$h]=n;next}b[$1]=n
END {for (i in a)if ((!(i in b))&&(i != "")) print i" in group file but not in passwd"
}' FS="(:)|(,)" /etc/group /etc/passwd
Cool, Klashxx I'm going to study your script. Thanks for the reply.
Thank you all for providing me the script to take care of what I need to do.
Thank you for everything... but I forgot to mention that I need to get rid of only users that starts with a alpha character followed by 4 digits (ie. A9999).
Can you do this with awk or do I have to use ksh?
Thanks.
Hi , u can use:
awk ' FNR==NR{for (h=4;h<=NF;h++)if(match($h,/^[A-Za-z][0-9]{4}$/))a[$h]=n;next}b[$1]=n
END {for (i in a)if ((!(i in b))&&(i != "")) print i" in group file but not in passwd"
}' FS="(:)|(,)" /etc/group /etc/passwd