From this I am getting nothing. The basic flow of this program (originally) is to capture the data needed in a log file and create a new file with a .sql extension. However, after adding the @list_files array and the for loop it doesn't seem to create any files. Any help here? I've searched for solutions and I'd prefer the ones that use the open or opendir instead of modules and the other ways.#!/usr/bin/perl use strict; use warnings; use POSIX 'strftime'; #my $filename = 'IGXLEventLog.3.17.2015.20.25.12.625.log'; my $directory = "/opt/lampp/htdocs/otpms/Data"; opendir (my $dir, $directory) or die "Could not open directory '$direc +tory': $!"; my @list_files; our $output; my %details; for my $filename (@list_files) { open(my $fn, '<', $filename) or die "Could not open file '$filenam +e': $!"; while(my $row = <$fn>) { chomp $row; if ($row =~ /Computer Name:\s*(\S+)/i ) # match computer name +with white space then non white space { $details{tester_name} = $1; } elsif ($row =~ /Operating System:\s*(.*\S)/i ) # match operati +ng system with white space then any word { $details{op_sys} = $1; } elsif ($row =~ /IG-XL Version:\s*([^;]*)/i ) # match ig-xl ver +sion with white space then semi colon { $details{igxl_vn} = $1; } elsif ($row =~ /^([\d.]+)\s+(\S+)(?=\s)/ ) #match slot with wh +ite space and then non white space { push @{$details{slot}}, $1; push @{$details{board_name}}, $2; } } close ($fn); my $timestamp = strftime '%Y-%m-%d.%H:%M:%S', localtime; $output = $timestamp .'.sql'; open(my $fh, '>', $output) or die "Could not create file '@output': $! +"; my $log_time_stamp = (stat($filename))[9]; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime +($log_time_stamp); my $nice_timestamp = sprintf ( "%04d-%02d-%02d %02d:%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min, +$sec); print $nice_timestamp; for (my $i = 0; $i < @{$details{slot}}; $i++) { print {$fh} "INSERT INTO TesterDeviceMatrix.TBL_TESTER_INFO" ."(tester_name, operating_system, version, board_name, config +, date_modified, log_created) " ."VALUES ('$details{tester_name}', '$details{op_sys}', '$detai +ls{board_name}[$i]', " ."'$details{igxl_vn}', '$details{slot}[$i]', '$timestamp', '$n +ice_timestamp');\n"; } close($fh); }
EDIT 2 I have decided to use readdir as suggested by one of the anonymous monks however, I am getting only one file and all the info captured from other files is combined into this one. Any help?:
This is the working code for one file only, with the name hard coded#!/usr/bin/perl use strict; use warnings; use POSIX 'strftime'; our $output; my %details; #my $filename = 'IGXLEventLog.3.17.2015.20.25.12.625.log'; my $directory = "/opt/lampp/htdocs/otpms/Data"; opendir (DIR, $directory) or die "Could not open directory '$directory +': $!"; my @files = readdir(DIR); closedir DIR; foreach my $filename (@files) { open(my $fn, '<', "$directory/$filename") or die "Could not open file +'$filename': $!"; while(my $row = <$fn>) { chomp $row; if ($row =~ /Computer Name:\s*(\S+)/i ) # match computer name +with white space then non white space { $details{tester_name} = $1; } elsif ($row =~ /Operating System:\s*(.*\S)/i ) # match operati +ng system with white space then any word { $details{op_sys} = $1; } elsif ($row =~ /IG-XL Version:\s*([^;]*)/i ) # match ig-xl ver +sion with white space then semi colon { $details{igxl_vn} = $1; } elsif ($row =~ /^([\d.]+)\s+(\S+)(?=\s)/ ) #match slot with wh +ite space and then non white space { push @{$details{slot}}, $1; push @{$details{board_name}}, $2; } } close ($fn); my $timestamp = strftime '%Y-%m-%d.%H:%M:%S', localtime; $output = $timestamp .'.sql'; open(my $fh, '>', "$directory/$output") or die "Could not create file +'$output': $!"; my $log_time_stamp = (stat($filename))[9]; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime +($log_time_stamp); my $nice_timestamp = sprintf ( "%04d-%02d-%02d %02d:%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min, +$sec); print $nice_timestamp; for (my $i = 0; $i < @{$details{slot}}; $i++) { print {$fh} "INSERT INTO TesterDeviceMatrix.TBL_TESTER_INFO" ."(tester_name, operating_system, version, board_name, config +, date_modified, log_created) " ."VALUES ('$details{tester_name}', '$details{op_sys}', '$detai +ls{board_name}[$i]', " ."'$details{igxl_vn}', '$details{slot}[$i]', '$timestamp', '$n +ice_timestamp');\n"; } close($fh); }
In reply to Opening multiple log files by hahazeeq
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |