arrays - Perl: Can I use pattern matching to find certain lines in a log file -


i have log file type of content:

mon nov 19 11:00:01 2012 host: myserver accurev-ent inuse: 629   mon nov 19 12:00:01 2012 host: myserver accurev-ent inuse: 629 

using perl, have figured out how remove empty lines , put non-empty lines array. i'm trying match present month, date , year. i.e., i'm trying grab of lines have may, 21, , 2013 (this file product of script runs everyday , 24 times each day. don't need hh:mm:ss data.

i keep trying pattern match in following vein:

foreach $prod (@prod) {   # sun may 19 02:00:01 2013   if ($prod =~ ((/sun may 19/) && $prod =~(/2013$/)) )   {     print "howdy! \n"; # using indicate success   } }   

can via pattern matching or should try split , find data match? way, once find match need put line containing inuse array , find largest number day.

#!/usr/bin/env perl use strict; use warnings; use posix qw(strftime);  # active regex looks today's date # commented out regex looks dates in current month # if provide suitable timestamp (seconds since epoch), # can generate pattern arbitrary date changing # time (a function call) $timestamp. $pattern = strftime("%b %d \\d+:\\d+:\\d+ %y", localtime(time)); # $pattern = strftime("%b \\d+ \\d+:\\d+:\\d+ %y", localtime(time)); # print "$pattern\n"; $regex = qr/$pattern/;  # @prod = <>;  foreach $prod (@prod) {     # print "check: $prod\n";     if ($prod =~ $regex)     {         print "$prod\n";     } } 

this uses strftime (from posix) create regex string current month , year in correct places, , handles strings of digits day , time components should be. creates quoted regex qr//, , applies each entry in @prod array. can make \d+ matches more rigid if wish; whether worth doing depends on cost of extraneous match. (one version of current regex more lenient be, recognizing 99th , 00th of may, , may 20130, etc; both allow invalid times through). these fixable tweaking regex without materially affecting answer.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -