Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Perl: Extracting specific text from a .txt file and outputting into a new format

by biohisham (Priest)
on Nov 18, 2010 at 14:39 UTC ( #872259=note: print w/ replies, xml ) Need Help??


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 .


Comment on Re: Perl: Extracting specific text from a .txt file and outputting into a new format
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://872259]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-07-26 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (178 votes), past polls