Home: Perl Programming Help: Regular Expressions:
Can anyone help me ?



eeduest
New User

Sep 5, 2014, 3:40 PM


Views: 26429
Can anyone help me ?

Hi everyone;

I'm stuck with the following and i would like to know if someone of you can help me. I'm trying to develop a script, the purpose is to match different counters from a log file and store them in a hash, in order to do that i need the correct regular expression to match for the below counters and their respective values:

==============

HD verbose statistics

Retried reads: 0
Retried writes: 3
Corrected sectors: 0
Write verify failures: 0
Interrupt timeouts: 0
HD busy timeouts: 0
HD busy delays: 0
HD max busy delay: 0
HD status errors: 0
HD corrected by disk: 0
Error handling restarts: 0

so i'm doing the following:

if (/HD\sverbose\sstatistics/){
$step =1;
}
elsif(($step eq 1) && (/^(\S+\s+\S+)\s+(\S+)/)){
my $counter =$1;
my $statistic =$2;
if ($counter ~~ @list){
$hash{$node}{$counter}{statistic} = $statistic;
}
}

The above code is fetching for the counters (Retried reads: Retried writes: Corrected sectors: Interrupt timeouts: ) and storing them in this hash: $hash{$node}{$counter}{statistic} = $statistic; the list "list" includes the counters mentioned

My regular expression is only fetching for words included in @list that have only one "space" in each of them, now i need a better regular expression to fetch for all counters (including those who have 2 or more spaces) and store them in the same hash, can someone help me?

Thanks in advance
/Ed


Chris Charley
User

Sep 6, 2014, 8:46 AM


Views: 26400
Re: [eeduest] Can anyone help me ?

If there is a colon in your data, maybe the following would work out for you.


Code
/^([^:]+):\s+(\d+)$/


This captures non-colon up to the colon, matches the colon and space(s) and finally captures the statistic.


FishMonger
Veteran / Moderator

Sep 6, 2014, 9:30 AM


Views: 26396
Re: [eeduest] Can anyone help me ?

Why not simply do a split on colon space? IMO it's cleaner and easier to maintain.


Code
my ($counter, $statistic) = split /:\s+/;



eeduest
New User

Sep 8, 2014, 6:01 AM


Views: 26306
Re: [Chris Charley] Can anyone help me ?

Thanks! that worked out well.
BR/
Ed