PERL help needed

Hi, I will be asking a series of major newbie questions, and you help is greatly appreciated in advance!!

I have to write a script that will parse a logfile in a directory, the directory name changes daily.

So far I have:

#!/usr/bin/perl
open LOGFILE,">logfile.txt";
($day, $month, $year) = (localtime)[3,4,5];
printf LOGFILE ("The current date is %04d.%02d.%02d\n", $year+1900, $month+1, $day);
close (LOGFILE);

The logfile for today for example is /app/syslog/logs/2013.07.02/[SERVERNAME]/messages

I was able to get the date format the same as the directory name with the code above. But now I want to get that full path into a variable.

Can someone help me how to create a variable with this path?

Thank you very much in advance!

use sprintf

#!/usr/bin/perl
open LOGFILE,">logfile.txt";
($day, $month, $year) = (localtime)[3,4,5];
printf LOGFILE ("The current date is %04d.%02d.%02d\n", $year+1900, $month+1, $day);
$path = "/app/syslog/logs/" . sprintf("%04d.%02d.%02d",$year+1900, $month+1, $day) . "/[SERVERNAME]/messages";
print $path;
close (LOGFILE);

$ perl date.pl 
/app/syslog/logs/2013.07.02/[SERVERNAME]/messages
1 Like

Thank you very much!

There will be several directories where I put [SERVERNAME] in the path. They will be respective to each server sending logs there.

At this point, I'd like to go through each of those directories, and look in each "messages" file. And check for words like "ERROR".

I'm thinking this should be done through some loop, but I need help with that.

Can you tell me how to parse each "messages" file in each directory and look for "ERROR"?

put all the server names in the server.txt and use the below while loop. (it's not perl script )

while read SERVERNAME
do
    grep "ERROR" /app/syslog/logs/$(date +%Y.%m.%d)/${SERVERNAME}/messages > /tmp/output.txt 2>&1
done < server.txt