How do I search a file for certain lines and then extract only certain sections of that line in Perl? -


so have text file server report , has 1000 lines of information in it. i'm trying write script can search report pieces of information i'm looking for. example:

server 1 health check

date - count of errors


06/25/15 : 14

6/24/15 : 21

6/23/15 : 17

6/24/15 : 33

server 2 health check

date - count of errors


06/25/15 : 4

6/24/15 : 13

6/23/15 : 21

6/24/15 : 33

errors caused x


server 1:

32

server 2:

24

the 3 sections "server health check 1", "server health check 2", , "errors caused x." data each section need extracted in bold. know how go doing this?

here's perl script:

#!/usr/bin/env perl                                                                       use warnings; use strict;  use constant section_health_check => 'health check'; use constant section_errors       => 'errors caused x'; use constant re_health_check_data => qr|^(\d+/\d+/\d+)\s+:\s+(\d+)|; # date, count       use constant re_errors_data       => qr|^(\d+)|;                     # count             use constant fmt_date             => '%02d-%02d-%02d';  open $fh, 'foo.txt' or die "unable open 'foo.txt' : $!";  # changeme                                   ($section, %errors_by_date, %errors_caused_by_server); $server = 0;  # read through file, line line                                                    while (<$fh>) {     next unless m|\s|; # skip empty / whitespace lines                                  $section =         m|${\section_health_check}| ? section_health_check       : m|${\section_errors}|       ? section_errors       :                               $section;      if (m|server (\d+)|) {         $server = $1;     }      if (section_health_check eq $section         , $_ =~ re_health_check_data) {         ($date, $count) = ($1, $2);         $errors_by_date{ $server }->{ date_to_yymmdd($date) } += $count;     }      if (section_errors eq $section         , $_ =~ re_errors_data) {         ($count) = $1;         $errors_caused_by_server{ $server } += $count;     } }  $server_id (sort {$a <=> $b} keys %errors_by_date) {     print "\n--- server $server_id ---\n\n";     $date (sort keys $errors_by_date{$server_id}) {         $count = $errors_by_date{$server_id}->{$date};         $normal_date = yymmdd_to_date($date);         print "on $normal_date there $count errors!\n";     }     $errors_count = $errors_caused_by_server{$server_id} // 0;     next unless $errors_count;     print "\nthere $errors_count errors caused server!\n"; }  sub date_to_yymmdd {     ($date) = @_;     ($mm,$dd,$yy) = split '/', $date;     return sprintf(fmt_date,$yy,$mm,$dd); }  sub yymmdd_to_date {     ($date) = @_;     ($yy,$mm,$dd) = split '-', $date;     return sprintf(fmt_date,$mm,$dd,$yy); }  1; 

which outputs following:

--- server 1 ---  on 06-23-15 there 17 errors! on 06-24-15 there 54 errors! on 06-25-15 there 14 errors!  there 32 errors caused server!  --- server 2 ---  on 06-23-15 there 21 errors! on 06-24-15 there 46 errors! on 06-25-15 there 4 errors!  there 24 errors caused server! 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -