Hi,
This is for Perl.
I have a while loop, and would like to process two files, the formats are given below :
access
access.<previousday>-*
How can perform a while loop on both logs, while creating the logic for access.<previousday>-* to get format like "access.20130615-124139" when trying to scan. Also, how I can put them within while loop.
while (<>) {
if (/string/){
do something...
do something...
}
Thanks.
man perlrun
mentions two loop options: perl -n
and perl -p with automatic print at the end of each line.
Example:
#!/bin/perl -n
if (/string/) {
do something...
}
Arguments are opening the files, so you can run this as
script.pl access*
knowing that the shell expands access* in alphabetic order,
or give them manually like
script.pl access access.date1 access.date2 ...
What are your access* files named?
Here are the file names :
access
access.20130516-*
for FILE in access.*
do
exec 5<access
exec 6<"$FILE"
while read LINE1 <&5 && read LINE2 <&6
do
...
done
exec 5<&-
exec 6<&-
done
Hi,
I need to put it into while loop.
Thanks.
Look closer, there's a while.
Is that syntax okay for perl?
No that's shell.
And IMHO you want to read the log files in sequence.
yes, first today's one and then previous day.
#! /usr/bin/perl -w
use strict;
use POSIX 'strftime';
# Grab yesterday's date in $yest_date
my $yest_date = strftime("%Y%m%d", localtime(time - 86400));
# Glob yesterday's filename in $yest_file
my $yest_file = glob "access.${yest_date}-*";
my $line = undef;
# Iterate through today's and yesterday's file (in the same sequence).
for ("access", $yest_file) {
# Open the file in consideration
open FH, "< $_";
# Read the file content line by line using while loop
while ($line = <FH>) {
<statements>
}
# Close the filehandle.
close FH;
}
Thank you for your response.
How do I run the command, either one gives me an error :
perl mfile.pl
perl mfile.pl access
readline() on unopened filehandle statements at mfile.pl line 19.