Hi all,
I am trying to read a colon delimited text file (filter.in) then
search for each field in another file (/etc/passwd) and if it is found
then write that line in the third file (passwd.out). Here is what I
have written so far but it is not given me the correct result. Thanks
for any help.
#!/bin/perl
#
# the format of filter.in is user1:user2:user3:user4:
#
use File::Copy;
use strict;
use warnings;
$|=1; # flush output buffer
open (FILTERfh, "< filter.in") || die "Can not open filter.in: $!\n";
open PASSWDfh, '</etc/passwd' or die "Can not open the file: $!\n";
open PASSWDFILfh, ">passwd.out";
while (<FILTERfh>) {
chomp;
my @input = split /:/, $_;
for (my $user = 1; $user <= $#input ; $user++) {
print "$input[$user] is being added.\n";
while (<PASSWDfh>) {
my %seen;
next if (m/^#/); # Skip comments
next if (m/^\s*$/); # Skip blank lines
my ($field1) = /([^:]+)/;
# print PASSWDFILfh $_ unless $seen{$field1} or warn
# "WARNING: User $input[$user] does not exist!\n";
print PASSWDFILfh $_ unless $input[$user] or warn
"WARNING: User $input[$user] does not exist!\n";
# print PASSWDFILfh $_ if("$field1" eq "$input[$user]");
# print PASSWDFILfh $_ if( grep(/$field1:/, $_ )) or warn
"WARNING: User $input[$user] does not exist!\n";
} # while
} # for
} # while
close FILTERfh;
close PASSWDFILfh;
close PASSWDfh;
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/