I am creating a script that I open inside of a specific folder, containing two different types of files. 'Secure' (secure.text, secure.001.text, etc.) files, and 'message' (message.txt, etc) files. Each file contains a log for the year, day, date, hour, and minute. I would like to be able to scan these files and create a new file that contains all log entries for January 2006. The purpose of this script is to be able to only scan one set of data (secure), and not all of it (messages).
I am having issues with my script finding the current working directory. To my understanding, perl will use the location of the script as the current directory. I am using the opendir and closedir functions, and getting prompted that there are 60 files within the directory of the script, but when I delete some files just to test it, it still says 60, leading me to believe that this is not using the current directory. It also is not finding 'secure', despite the correct regex.
The output I am getting is "Issue finding file 'secure'."
#!/usr/bin/perl
use strict;
use warnings;
#Locate and scan all of the files
#list all files in same dir as this perl script.
my $dirname = ".";
opendir( my $dh, $dirname ) #sets $dh as the current working directory
or die "Can't open dir '$dirname':$!\n"; #Kills if can not open current directory.
my @all_the_file; #Instantiates new variable that accounts for all of the files in the current dir.
while( my $file = readdir($dh) ) { #$file accounts for every file on device.
push( @all_the_file, $file ); #Pushes files in current directory to $file.
}
closedir( $dh );
#Gets all of the secure files.
my @all_the_secure_file;
@all_the_secure_file = grep(/^secure(\.\d{1,2})?$/, @all_the_file);
#Itterate over secure files.
my $filename = "secure";
open( my $fhin, "<", $filename)
or die "Can't open '$filename':$!\n";
chomp( my @lines = <$fhin> ) ;
close($fhin);
#Match the Regex of Jan.
my @jan_lines = grep( /^Jan/ , @lines ) ;
print "The file '$filename' = " . @lines . " lines.\n";
print "Size of \@jan_lines = " . @jan_lines . "\n";
#Print and create new file with Data.
my $filename2 = "secure.1";
open( my $fhin, "<", $filename)
or die "Can't open '$filename':$!\n";
chomp( my @lines2 = <$fhin> ) ;
close($fhin);
my @jan_lines2 = grep( /^Jan/ , @lines2 ) ;
print "The file '$filename2' = " . @lines2 . " lines.\n";
print "Size of \@jan_lines2 = " . @jan_lines2 . "\n";
exit;