[PLUG] shell scripting problem

Paul Heinlein heinlein at attbi.com
Fri Jun 7 14:30:39 UTC 2002


On Thu, 6 Jun 2002, Matt Alexander wrote:

> I have about 200 names in /etc/group that I want to remove.  
> They're all under a generic "users" group.  I have a file that
> contains the names that I want to remove, and I'm looking for a
> wizbangwowzerz *nix way to read in all those names and delete them
> from the group file.  I'll also need to delete the comma after each
> name.

[Yeah, Mike, I'm being predictable... :-)]

I'd use perl, just because sed doesn't seem to me as clean about
differentiating words from substrings.

#!/usr/bin/perl -w

use strict;

my $tmpoutput = '/tmp/group';
my @goners = ();

open( GONERS, "< /list/of/oldusers.txt" ) or die $!;
while (<GONERS>) {
  chomp;
  push @goners, $_;
}
close( GONERS ) or die $!;

open( TMPOUT, "> $tmpoutput" ) or die $!;

open( GROUP, "< /etc/group" );
while ( <GROUP> ) {
  foreach my $user ( @goners ) {
    # don't delete whole groups, only users
    next if /^$user:/;
    s/\b$user,?\b//;
  }
  s/,$//;
  print TMPOUT $_;
}
close( GROUP ) or die $!;

close( TMPOUT ) or die $!;

exit;

#
# eof
#

--Paul Heinlein <heinlein at attbi.com>






More information about the PLUG mailing list