Friday, 23 January 2015

Perl data collation

When you need to gather data from many different sources and combine it in to one table of data there are a few ways to do it. The pr command can take data from many files and convert them to one table. Awk is a good choice also. Here is how I did it using Perl.

Here is the sample input data.
server1
09:53:01 94
09:53:02 96
09:53:03 95
server2
09:53:01 94
09:53:02 96
09:53:03 95
server3
09:53:01 94
09:53:02 96
09:53:03 95
server4
09:53:01 94
09:53:02 96
09:53:03 95
server5
09:53:01 94
09:53:02 96
09:53:03 95
server6
09:53:01 94
09:53:02 96
09:53:03 95
Here is the code. Save it as a Perl file and pipe your data into it.

#!/usr/bin/perl

while (<>) {
 @data = split(/\t/);
 if($data[0] =~ m/^\d/) {  
  $Times{$data[0]} = 1;
  chomp($data[1]);
  $Result{$Server}{$data[0]} = $data[1];
 } else {
  chomp($data[0]);
  $Servers{$data[0]} = 1;
  $Server = $data[0];
 }
}
printf "Time     \t";
foreach $S (keys %Servers) {
 printf "$S\t";
}
print "\n";
foreach $T (keys %Times) {
 print "$T\t";
 foreach $S (keys %Servers) {
  printf "$Result{$S}{$T}\t";
 }
 print "\n";
}

Here is your output.
Time      server3 server1 server2 server5 server4 server6 
09:53:02 96 96 96 96 96 96 
09:53:03 95 95 95 95 95 95 
09:53:01 94 94 94 94 94 94 

No comments:

Post a Comment