Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to clean up /etc/group file 2

Status
Not open for further replies.

pdtak

Technical User
Feb 25, 2008
63
US
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.
 
Here is an awk script that removes nonexistent group users:

Code:
awk '
        BEGIN { FS=OFS=":" }
        FNR==NR { username[$1]=1 ; next }
        {
                n=split($4,groupuser,",")
                $4=sep=""
                for (i=1; i<=n; i++) {
                        if (groupuser[i] in username) {
                                $4=$4 sep groupuser[i]
                                sep=","
                        }
                }
                print
        }
' passwd group

Annihilannic.
 
Annihilannic,

You're the best! Thanks a bunch!!!

This statement prints it out, but if I wanted to actually write out the cleaned up version to another file, what would I need to do, ie. group.out.

Thanks again.
 
Annihilannic,

Didn't quite work for me... it didn't remove the entries from the original group file. Am I missing something?

Thanks.

 
This statement prints it out, but if I wanted to actually write out the cleaned up version to another file, what would I need to do, ie. group.out.

Simply redirect the output to a new file, and then replace the original group file with it. Or if you prefer, back up the original group file, and use it as the input, e.g.

[tt]cp -p /etc/group /etc/group.backup
awk '<insert script here>' /etc/passwd /etc/group.backup > /etc/group[/tt]

Didn't quite work for me... it didn't remove the entries from the original group file. Am I missing something?

I don't know, can you show me an example line from the group file that was not updated, and let me know which user(s) should have been removed from that line?

Annihilannic.
 
Annihilannic,

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.
 
Yes, you can do this easily with awk with some slight modifications to that script. Have a look at the man page for awk, specifically the match() function.

Annihilannic.
 
get rid of only users that starts with a alpha character followed by 4 digits
Replace this:
if (groupuser in username) {
with this:
if ((groupuser in username) || groupuser!~/[A-Za-z][0-9][0-9][0-9][0-9]/) {

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top