1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #!/usr/bin/perl
use strict;
use warnings;
use Term::ANSIColor;
use List::Util qw( max );
my %informations;
my $max_sec = 0;
my $fichier = $ARGV[0];
#Test
if ( $fichier eq "" ) {
print colored ['red on_black'], 'syntaxe : Tri_matlab.pl nom_de_fichier', "\n";
exit 1;
}
if ( defined( $ARGV[1] ) ) {
print colored ['red on_black'], 'This script can not load one more one file', "\n";
exit 1;
}
my @rootName = split( /[.]/, $fichier );
my $CSVFILE = "$rootName[0].csv";
open F, '<', $fichier || die "Impossible de lire le fichier : $fichier\n";
while ( my $ligne = <F> ) {
chomp $ligne;
# Si la ligne contient Flag time :, on mémorise les informations nécessaires et supprime le flag
if ( $ligne =~ s{^Flag time\s+:\s+}{}i ) {
my ( $pid, $temps, $login, $data2, $pourcentage ) = split /\s+/, $ligne;
my ( $heure, $minute, $seconde ) = split /:/, $temps;
my $time = ( $heure * 3600 ) + ( $minute * 60 ) + $seconde;
# on prend l'index sur le PID avec la valeur max temps
if ( !$informations{$pid} ) { $max_sec = 0; }
#grace à l fonction max on prend la valeur max par rapport à $time
$max_sec = max( $max_sec, $time );
$informations{$pid}{temps} = $max_sec;
$informations{$pid}{login} = $login;
}
}
close(F);
# Affichage des informations mises en mémoire avec Tri
open REPORTFILE, '>', $CSVFILE;
foreach my $unpid ( sort { $informations{$a}{temps} <=> $informations{$b}{temps} } keys %informations ) {
my $sec = $informations{$unpid}{temps};
my $login = $informations{$unpid}{login};
print "PID : $unpid Temps: $sec Login $login\n";
print REPORTFILE "PID : $unpid Temps: $sec Login $login\n";
# sprintf( 'PID:%7 Temps: %5 Login: %10', $unpid , $sec, $login) ) ;
}
close REPORTFILE |
Partager