hahazeeq has asked for the wisdom of the Perl Monks concerning the following question:
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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Opening multiple log files
by kcott (Archbishop) on Jun 15, 2015 at 07:00 UTC | |
by hahazeeq (Novice) on Jun 15, 2015 at 07:10 UTC | |
by kcott (Archbishop) on Jun 15, 2015 at 07:47 UTC | |
by hahazeeq (Novice) on Jun 15, 2015 at 08:06 UTC | |
by kcott (Archbishop) on Jun 15, 2015 at 08:33 UTC | |
| |
by Anonymous Monk on Jun 15, 2015 at 09:14 UTC | |
|
Re: Opening multiple log files
by Laurent_R (Canon) on Jun 15, 2015 at 06:56 UTC | |
by hahazeeq (Novice) on Jun 15, 2015 at 07:01 UTC | |
by marinersk (Priest) on Jun 15, 2015 at 16:47 UTC | |
|
Re: Opening multiple log files
by sandy105 (Scribe) on Jun 15, 2015 at 08:43 UTC | |
by Anonymous Monk on Jun 15, 2015 at 08:48 UTC | |
by sandy105 (Scribe) on Jun 15, 2015 at 09:07 UTC | |
|
Re: Opening multiple log files
by bulrush (Scribe) on Jun 15, 2015 at 11:28 UTC | |
|
Re: Opening multiple log files
by Anonymous Monk on Jun 15, 2015 at 07:33 UTC | |
by hahazeeq (Novice) on Jun 15, 2015 at 07:43 UTC | |
by soonix (Chancellor) on Jun 15, 2015 at 08:28 UTC | |
|
Re: Opening multiple log files
by Anonymous Monk on Jun 15, 2015 at 08:39 UTC |