Link to home
Start Free TrialLog in
Avatar of new_perl_user
new_perl_user

asked on

Reading log file in Perl

Hi,
Can anyone please help me with the code snippet for the below issue:

Need to the read the attached "Test_log" file on a daily basis based on date  and generate the output as shown below into a log file. Next login to Db and run a select query statement and generate a log file with the query results.
Finally send an email with both the log files attachment and the  total count of results in the log files  as subject line

[b]output for the log file attached should be:[/b]
HEFFILE0004/02720220R
HEFFILE0004/60530610R
HEFFILE0004/60530700R
HEFFILE0004/60531070R

Open in new window

Test-log.txt
Avatar of jeromee
jeromee
Flag of United States of America image

you would need to be more specific but in term of the rules governing what to generate in the log file and more importantly "the login to Db and run a select query statement and generate a log file with the query results". This is so vague that it makes my head spin.
Avatar of new_perl_user
new_perl_user

ASKER

I  am sorry.

1)Below is the  file I need to read daily and grab yesterday's data from that log file based on date and then write the ouput  to  daily.txt.

Test_log sample  Data:

##################### SYNC  STARTED: Mon Sep 26 03:10:05
 #####################
receiving file list ... done
.d..t.... HEFFILE0004/02720220R/METADATA/
.d..t.... HEFFILE0004/60530610R/METADATA/
.d..t.... HEFFILE0004/60530700R/METADATA/
.d..t.... HEFFILE0004/60531070R/METADATA/
.d..t.... HEFFILE0004/60910750R/METADATA/
##################### SYNC FINISHED: Mon Sep 26 05:15:39 EDT 2011  #####################

Open in new window


Output Format for daily.txt :
HEFFILE0004/02720220R
HEFFILE0004/60530610R
HEFFILE0004/60530700R
HEFFILE0004/60531070R


2) After that login to Oracle Database and the run the below query:
Select a,b from table1 where date =to_date(SYSDATE, 'DD.MM.YY')-1;  and write the output to a Db.txt


3) finally send an email with both the logs attached (daily.txt , Db.txt). If possible can we get the count  of data in each log file as subject line of an email  like( daily.txt = 10 files , db.txt = 20 files)

If I am not clear yet please let me know. Thank you for  the  help.
can anyone please help me on this..
any suggestions will be very helpful

Thanks,
Hi Jeromee,
If possible can you please help me with #1. I am able to do the #2  and will try out #3.

Thanks,
Here's #1 only...

run as
   perl thisscript.pl  Test.log.txt

Please note that I made some assumption re: what the filename must look like (HEFFILEnnnn/yyyyyy/)
use strict;

my $yesterday = scalar localtime(time()-24*60*60);	# Thu Sep 29 14:59:20 2011
substr($yesterday, 11, 9) = "";				# remove the time
my $date = '';
while( <> ) {
   if( /SYNC  STARTED: (.*) \d\d:\d\d:\d\d .* (\d{4})/ ) {	# Extract the date of each sectio
      $date = "$1 $2";
   }

   if( $yesterday eq $date && m{(HEFFILE\d+/[^/]+)} ) {		# if the same date as yesterday and has the right filename signature
      print "$1\n";
   }
}

Open in new window

Hi,

Thank you so much for the response.
 
I tried the above code but it is not printing anything. Can you please take a look. Below is the sample Test.log.txt I am trying to read.

##################### SYNC  STARTED: Thu Sep 29 03:10:05 EDT 2011 #####################
receiving file list ... done
.d..t.... HEFFILE0004/02720220R/METADATA/
.d..t.... HEFFILE0004/60530610R/METADATA/
.d..t.... HEFFILE0004/60530700R/METADATA/
.d..t.... HEFFILE0004/60531070R/METADATA/
.d..t.... HEFFILE0004/60910750R/METADATA/
##################### SYNC FINISHED: Thu Sep 29 05:15:39 EDT 2011 #####################

Open in new window

It works for me:

HEFFILE0004/02720220R
HEFFILE0004/60530610R
HEFFILE0004/60530700R
HEFFILE0004/60531070R
HEFFILE0004/60910750R

How do you run the script?
My bad it is working now. Executed in a wrong way.  One small change needed if possible. Right now it is printing all the files based on date, even if there are repetitive filenames like

HEFFILE0004/02720220R
HEFFILE0004/02720220R
HEFFILE0004/60530610R
HEFFILE0004/60530700R

 So it is possible to get just only one entry for repetitive data and write the output to a log file.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of jeromee
jeromee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
can we write the output to a log file.
perl thisscript.pl  Test.log.txt > output.txt
Thanks a ton for the help.
no problem.
Happy Perling!
Hi Jeromee,

I was running the script again today to grab for data from yesterday(oct 2) but it is not working . Any ideas or suggestion on why??
Never mind.. It is working, there were spaces near the date so it failed. Removed those and it is fine..

Thanks,