开发者

perl report formatting using printf - skip over some lines.

开发者 https://www.devze.com 2023-03-25 17:59 出处:网络
I have a output that looks like this : BEGIN newcomm stat for rufarm102 __________________________________________________________________________________

I have a output that looks like this :

BEGIN newcomm stat for rufarm102
__________________________________________________________________________________

ibfarm102 localtick     Boston       hibmis100 procHKHD2  Hongkong   PidMonRsp
ibfarm102 localtick     London       stopsrv1 proceu8  Zug   PidMonRsp
ibfarm102 localtick     Greenwich       hibmis100 procHKHD2  Hongkong   PidMonReq
ibfarm102 localtick     New York      stopsrv1 proceu8  Zug   PidMonReq

END : ibfarm102  newcomstat

and I run it through this script:

#!/usr/bin/perl
open (REPORT , "/home/data/report20110801130041");
while (<REPORT>){
  if ($_ =~ /BEGIN/){
     print $_ ;
  }
  if ($_ =~ /END/){
     print $_;
  }
  if ($_ =~ /OCOMsg2/){
     use Term::ANSIColor;
     print colo开发者_StackOverflow社区r 'red';
     ($config_host , $config_process, $config_region, $stat_host, $stat_process, $stat_host_region, $stat_message_class) = split(/\s+/, $_ );
     printf "%-15s %-10s %10s %15s %-10s %-8s %10s", $config_host , $config_process, $config_region, $stat_host, $stat_process, $stat_host_region, $stat_message_class ;
     print color 'reset';
     print "\n";
  }
  else{
  ($config_host, $config_process, $config_region, $stat_host, $stat_process, $stat_host_region, $stat_message_class) = split(/\s+/, $_ );
  printf "%-10s %-10s %10s %15s %-10s %-8s %10s", $config_host , $config_process, $config_region, $stat_host, $stat_process, $stat_host_region, $stat_message_class ;
  print "\n";
  }

}

I make an effort to skip the BEGIN and END lines - but they get printed out twice - and i have tried skipping over the BEGIN and END a few times, a few different ways. I dont need the BEGIN and END lines split and formatted by printf.

BEGIN newcomm stat for ibfarm102
BEGIN      newcomm          stat             for ibfarm102   

ibfarm102  localtick   Greenwich       hibmis100 procHKHD2  Hongkong  PidMonRsp
ibfarm102  localtick   Greenwich        stopsrv1 proceu8    Zug       PidMonRsp
ibfarm102  localtick   Greenwich       hibmis100 procHKHD2  Hongkong  PidMonReq
ibfarm102  localtick   Greenwich        stopsrv1 proceu8    Zug       PidMonReq

END : ibfarm102  newcomstat
END        :           ibfarm102      newcomstat


Your second and third ifs should be elsif; otherwise, the BEGIN and END print out once and then again in the else clause (since as is that else catches everything that isn't OCOMsg2).


Add next into the blocks handling matching BEGIN and END:

while(<REPORT>) {
    if (/BEGIN/) {
        print;
        next;
    }
...

This will make it start the next iteration of the while loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消