我有一個輸出看起來像這樣:使用printf的perl報告格式 - 跳過一些行。
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
,我通過這個腳本運行:
#!/usr/bin/perl
open (REPORT , "/home/data/report20110801130041");
while (<REPORT>){
if ($_ =~ /BEGIN/){
print $_ ;
}
if ($_ =~ /END/){
print $_;
}
if ($_ =~ /OCOMsg2/){
use Term::ANSIColor;
print color '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";
}
}
我努力跳過開始和結束行 - 但他們打印出兩次 - 我試過跳過B EGIN和END幾次,有幾種不同的方式。我不要 需要將BEGIN和END行拆分並由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
我不認爲你可以分割空白,因爲「紐約」將不會被正確打印。看起來更像固定寬度的數據,這意味着正則表達式或者打包/解包。可能由製表符分隔。 – TLP