Advertisement
Guest User

log2spreadsheet.pl

a guest
Mar 4th, 2012
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.77 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. # Name: log2spreadsheet.pl
  3. # gepostet hier:
  4. # Funktion: Daten, die mit zukunfts_stats_threaded.pl erstellt wurden, in CSV-Format umwandeln
  5. #
  6. # Hinweise des Autors:
  7. # das log2spreadsheet script einfach via
  8. # perl log2spreadsheet.pl > import_me.csv
  9. # starten ... das import_me.csv kann man dann einfach
  10. # in open office importieren und mit 2 mausclicks einen schönen chart zaubern.
  11.  
  12. use strict;
  13. use warnings;
  14.  
  15. my %timestamps = ();
  16. my %ids = ();
  17.  
  18. my $dirname = './logs';
  19. opendir(DIR,$dirname) || die $! . "\n";
  20. while(my $file = readdir(DIR)){
  21.  next if $file =~ /^\.+$/;
  22.  #print $file . "\n";
  23.  open(IN,$dirname . '/' . $file) or die $! . "\n";
  24.  while(<IN>){
  25.   chomp();
  26.   if($_ =~ /^TIME:(\d+)\/ID:(\d+)\/COUNT:(\d+)/){
  27.    my($time,$id,$count)=($1,$2,$3);
  28.    $timestamps{$time}{$id}=$count;
  29.    $ids{$id}++;
  30.   }
  31.  }
  32.  close(IN);
  33. }
  34. closedir(DIR) or warn $! . "\n";
  35.  
  36. my @ids=sort({$a<=>$b}keys %ids);
  37. print '"time",' . join(',',@ids) . "\n";
  38.  
  39. foreach my $timestamp(sort{$a<=>$b}(keys %timestamps)){
  40.  print '"' . t2t($timestamp) . '",';
  41.  my @tmp=();
  42.  foreach my $id (@ids){
  43.   my $value = defined $timestamps{$timestamp}{$id} ? $timestamps{$timestamp}{$id} : '';
  44.   push(@tmp,$value);
  45.  }
  46.  print join(',',@tmp);
  47.  print "\n";
  48. }
  49.  
  50. sub t2t {
  51.  my($unixtime)=@_;
  52.  my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($unixtime);
  53.  $mon++;
  54.  $year+=1900;
  55.  my $timestamp = $mday . '.' . $mon . '.' . $year . ' ' . $hour . ':' . $min . ':' . $sec;
  56.  return $timestamp;
  57. }    
  58.  
  59.  
  60.     # 0 1 2 3 4 5 6 7 8
  61. #    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  62. #    localtime(time);
  63.    
  64.    
  65.  #   my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
  66.  #   print "$abbr[$mon] $mday";
  67.     # $mon=9, $mday=18 gives "Oct 18"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement