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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$_ troubles

Status
Not open for further replies.

swabs

IS-IT--Management
Jul 28, 2003
155
US
Any help figuring out the logic would be appreciated.

I have 2 files
names.log and report.rpt

names.log contains:
tomjr
billsv
terrytv


report.rpt contains:
tomjr logged in at 10am
puppychow logged in at 11am
billsv logged in at 12pm
kittycat logged in at 1pm


I want to parse the report.rpt file. If a user from names.log is in the report.rpt file then print the entire line to a 3rd file(output.log)

I will post my code below. The problem I am having is the nested $_ in the foreach loop.

ARGV[0]=names.log
ARGV[1]=report.rpt
ARGV[2]=output.log


############################################################
open(OUTPUTLOG,">>$ARGV[2]") || die "Couldn't create the file\n";

my $userlist="$ARGV[0]";
open (ULIST, $userlist);
chomp(@userlist=<ULIST>);


foreach $_ (@userlist) {



open (DATA, "$ARGV[1]") || die "Couldn't open file\n";

while (<DATA>) {

if (m/$_/) {print }

}

}
close DATA;
close OUTPUTLOG;


########################################################
 
oops.

Here is the full code of the file


##############################################################
open(OUTPUTLOG,">>$ARGV[2]") || die "Couldn't create the file\n";

my $userlist="$ARGV[0]";
open (ULIST, $userlist);
chomp(@userlist=<ULIST>);


foreach $_ (@userlist) {



open (DATA, "$ARGV[1]") || die "Couldn't open file\n";

while (<DATA>) {

if (m/$_/) {print }

}

}
close DATA;
close OUTPUTLOG;





 
just using your existing code:

Code:
open(OUTPUTLOG,">>$ARGV[2]")    || die "Couldn't create the file\n";

my $userlist = $ARGV[0];

open (ULIST, $userlist);
chomp(@userlist=<ULIST>);
close(ULIST);

foreach [b]my $line[/b] (@userlist) {
   open (DATA, "$ARGV[1]") || die "Couldn't open file\n";
   while (<DATA>) {
      if (/[b]$line[/b]/) {print  }
   }
}
close DATA;
close OUTPUTLOG;





- Kevin, perl coder unexceptional! [wiggle]
 
swabs, there's a problem with algorithm that you supplied (and Kevin corrected) - you're going to iterate through your log file for every user in the user list. For a small number of users and a short log file, that won't be a problem - but as the user list and/or the log file gets larger, that's going to slow things down. Consider using a hash to see whether the user is valid or not:
Code:
my ($userfile, $logfile, $outputfile) = @ARGV[0..2];

open OUTPUT, "> $outputfile" or die "Cannot open $outputfile for output\n$!";

open USERS, "< $userfile" or die "Cannot read $userfile\n$!";
my %users = map {chomp; ($_,1)} <USERS>;
close USERS;

open LOG, "< $logfile" or die "Cannot read $logfile\n$!";
while (<LOG>) {
    m/^\s*(\w+)/;
    print(OUTPUT $_) if $users{$1};
}
close LOG;
close OUTPUT;
If grouping the output by user is important (so that all the logins by, say, tomjr are printed together) you can modify this code a bit.
 
KevinADC and Rharsh.

Thank you both very much. It is nice to have a quick fix to finish the project. It is also nice to see a more efficient way to solve the issue.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top