http://www.perlmonks.org?node_id=872259


in reply to Perl: Extracting specific text from a .txt file and outputting into a new format

This should get you started, we don't know before hand whether a given line has the log entry that we need to capture but we assume that the log entries follow a certain order that may be intermittent by other uninteresting log entries (i.e each new log record starts with the 'System' entry). In the code below I am capturing the server name, the bytes in and the bytes out into a hash of incremental records, you can extend on that to capture the rest of the entries you seek..
#!/usr/local/bin/perl use strict; use warnings; use Data::Dump qw(pp); my %log; my $record = 0; #to be incremented on each round while(my $line = <DATA>){ chomp $line; my ($system,$server,$b_in,$byteIn,$b_out, $byteOut); if($line =~ /System/){ $record++ ; ($system,$server) = split / : /,$line; push @{$log{'record'.$record}},{$system=>$server} }elsif($line =~ /ByteIn/){ ($b_in, $byteIn) = split / : /,$line; push @{$log{'record'.$record}},{$b_in=>$byteIn +} ; }elsif($line =~ /Byteout/){ ($b_out, $byteOut) = split / : /,$line +; push @{$log{'record'.$record}},{$b_out +=>$byteOut} } } print pp \%log; __DATA__ System : system001-server Box : HW a2b 1234Mb 123.4, address is 0012.d345.1234 (abc 0012.d123 +. +4567) Byteout : 5816943852464 ByteIn : 4 Description : example_system_server1_example System : system002-server Box : HW a2b 1234Mb 123.4, ByteIn : 3385 Byteout : 58169 Description : example_system_server2_example
OUTPUT
{ record1 => [ { System => "system001-server" }, { Byteout => "5816943852464" }, { ByteIn => 4 }, ], record2 => [ { System => "system002-server" }, { ByteIn => 3385 }, { Byteout => 58169 }, ], }
read Data::Dump, Data Structures Cookbook (perldsc), perlref


Excellence is an Endeavor of Persistence. A Year-Old Monk :D .