on 4/30/02 4:28 AM, [EMAIL PROTECTED] purportedly said:

> The list is in a file called "france" and contains users name like:
> 
> miko
> martha
> 
> The file to control is the /etc/passwd:
> 
> miko:Sn83kdmcsd4ee:816:1005:MirceaKovari:/tmp:/bin/selfa
> nikol:Sn83kdmcsd4ee:816:1005:NiklaOlman:/tmp:/bin/selfa
> 
> I need to put in a new file the line of user
> "miko"(miko:Sn83kdmcsd4ee:816:1005:MirceaKovari:/tmp:/bin/selfa) that is
> contained in the list(file france) but not "nikol" which is not contained
> and so on for ol the list and all the /etc/passwd file.Thanks

It's fairly simple. The most flexible way, which assumes that the names in
"list" are not necessarily in the same order as "passwd":

# read in list file
open LIST, "france" or die "$!";
@list = <LIST>;         # read in whole file
chomp( @list );         # remove trailing newines
close LIST;

# read passwd
open PASS, "/etc/passwd" or die "$!";
@passwd = <PASS>;         # read in whole file
close PASS;

@new_passwd = ();       # create array to hold matching entries

foreach $name ( @list ) {

  foreach $entry ( @passwd ) {

    if( $entry =~ /^$name:/ ) {
      push( @new_passwd, $entry );
      last;         # exit loop
    }

  }

}

# now @new_passwd has all matching entries from /etc/passwd.
# It can be written to a file, or whatever.

# write to file
open FILE, ">passwd.new" or die "$!";
print FILE @new_passwd;
close FILE;

__END__

Keary Suska
Esoteritech, Inc.
"Leveraging Open Source for a better Internet"

Reply via email to