OVH Cloud OVH Cloud

Filtrer les lignes d'un tableau

3 réponses
Avatar
gniagnia
bonjour,

Voici mon script :


#!/usr/bin/perl

$list_command="ls -la /tmp/log*";

open(DIR, " $list_command | " ) || die " died " ;
@list_logs = <DIR> ;
closedir DIR;
print @list_logs;




Le resultat est le suivant :

[root@testsrv]# ./test.pl
-rw-r--r-- 1 root root 0 Feb 23 04:02 /tmp/log
-rw-r--r-- 1 root root 0 Feb 22 04:02 /tmp/log.1
-rw-r--r-- 1 root root 0 Feb 21 04:02 /tmp/log.2
-rw-r--r-- 1 root root 370 Feb 21 00:24 /tmp/log.3
-rw-r--r-- 1 root root 102 Feb 19 15:13 /tmp/log.4



J'aimerai en fait que l'output soit le suivant :

log Feb 23 04:02
log.1 Feb 22 04:02
log.2 Feb 21 04:02
log.3 Feb 21 00:24
log.4 Feb 19 15:13


Etant donne que je suis un enorme noob en perl...je ne sait pas du
tout comment m'y prendre.

Merci d'avance pour votre aide :)

3 réponses

Avatar
tfe
Tu dois utiliser un remplacement par experssion régulière:
Pour chaque ligen du tableau, remplacer la ligne par juste certains
éléments de celle-ci.

foreach(@list_logs)
{
s!^(?:S+s+){5}(S+s+S+s+S+)s+/(S+)!$2 $1!;
}

Je te laisse lire perldoc perlretut pour découvrir les regex par
contre :)

--
tfe
http://tfeserver.be

On 23 fév, 10:44, "gniagnia" wrote:
bonjour,

Voici mon script :

#!/usr/bin/perl

$list_command="ls -la /tmp/log*";

open(DIR, " $list_command | " ) || die " died " ;
@list_logs = <DIR> ;
closedir DIR;
print @list_logs;

Le resultat est le suivant :

[]# ./test.pl
-rw-r--r-- 1 root root 0 Feb 23 04:02 /tmp/log
-rw-r--r-- 1 root root 0 Feb 22 04:02 /tmp/log.1
-rw-r--r-- 1 root root 0 Feb 21 04:02 /tmp/log.2
-rw-r--r-- 1 root root 370 Feb 21 00:24 /tmp/log.3
-rw-r--r-- 1 root root 102 Feb 19 15:13 /tmp/log.4

J'aimerai en fait que l'output soit le suivant :

log Feb 23 04:02
log.1 Feb 22 04:02
log.2 Feb 21 04:02
log.3 Feb 21 00:24
log.4 Feb 19 15:13

Etant donne que je suis un enorme noob en perl...je ne sait pas du
tout comment m'y prendre.

Merci d'avance pour votre aide :)


Avatar
kurtz le pirate
In article ,
"gniagnia" wrote:

bonjour,

Voici mon script :


#!/usr/bin/perl

$list_command="ls -la /tmp/log*";

open(DIR, " $list_command | " ) || die " died " ;
@list_logs = <DIR> ;
closedir DIR;
print @list_logs;




Le resultat est le suivant :

[]# ./test.pl
-rw-r--r-- 1 root root 0 Feb 23 04:02 /tmp/log
-rw-r--r-- 1 root root 0 Feb 22 04:02 /tmp/log.1
-rw-r--r-- 1 root root 0 Feb 21 04:02 /tmp/log.2
-rw-r--r-- 1 root root 370 Feb 21 00:24 /tmp/log.3
-rw-r--r-- 1 root root 102 Feb 19 15:13 /tmp/log.4



J'aimerai en fait que l'output soit le suivant :

log Feb 23 04:02
log.1 Feb 22 04:02
log.2 Feb 21 04:02
log.3 Feb 21 00:24
log.4 Feb 19 15:13


Etant donne que je suis un enorme noob en perl...je ne sait pas du
tout comment m'y prendre.

Merci d'avance pour votre aide :)



je ferais un truc comme ça :
--------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;


my $thisDir="/var/log";
chdir($thisDir) || die "can't chdir $!";

my $cmd=qx/ls -l system.log*/;

my @vals;
my @allFiles=split /n/,$cmd;
foreach my $oneFile (@allFiles) {
$oneFile =~ s/s+/ /g;
@vals = split / /,$oneFile;
print "$vals[8] $vals[5]_$vals[6]_$vals[7]n";
}
--------------------------------------------------------------------

pour l'exemple : '/var/log' et les fichiers 'system.log*'

le shell :
#:ls -l system.log*
-rw-r----- 1 root admin 1040329 23 Feb 17:53 system.log
-rw-r----- 1 root admin 96599 17 Dec 10:31 system.log.0.gz
-rw-r----- 1 root admin 48014 7 Oct 18:00 system.log.1.gz
-rw-r----- 1 root admin 8933 8 Sep 19:19 system.log.2.gz
-rw-r----- 1 root admin 128624 2 Sep 13:57 system.log.3.gz
-rw-r----- 1 root admin 13966 7 May 2006 system.log.4.gz
-rw-r----- 1 root admin 77372 30 Apr 2006 system.log.5.gz
-rw-r----- 1 root admin 26589 11 Mar 2006 system.log.6.gz
-rw-r----- 1 root admin 35979 19 Feb 2006 system.log.7.gz

le perl :
system.log 23_Feb_17:53
system.log.0.gz 17_Dec_10:31
system.log.1.gz 7_Oct_18:00
system.log.2.gz 8_Sep_19:19
system.log.3.gz 2_Sep_13:57
system.log.4.gz 7_May_2006
system.log.5.gz 30_Apr_2006
system.log.6.gz 11_Mar_2006
system.log.7.gz 19_Feb_2006


éventuellement modifier les indices dans le print "$vals[8]...";

ne pas oublier : TIMTOWTDI :)







--
klp

Avatar
Benoit Izac
Bonjour,

le 23/02/2007 à 10:44, "gniagnia" a écrit dans le
message :

#!/usr/bin/perl

$list_command="ls -la /tmp/log*";

open(DIR, " $list_command | " ) || die " died " ;
@list_logs = <DIR> ;
closedir DIR;
print @list_logs;


Le resultat est le suivant :

[]# ./test.pl
-rw-r--r-- 1 root root 0 Feb 23 04:02 /tmp/log
-rw-r--r-- 1 root root 0 Feb 22 04:02 /tmp/log.1
-rw-r--r-- 1 root root 0 Feb 21 04:02 /tmp/log.2
-rw-r--r-- 1 root root 370 Feb 21 00:24 /tmp/log.3
-rw-r--r-- 1 root root 102 Feb 19 15:13 /tmp/log.4


J'aimerai en fait que l'output soit le suivant :

log Feb 23 04:02
log.1 Feb 22 04:02
log.2 Feb 21 04:02
log.3 Feb 21 00:24
log.4 Feb 19 15:13


#!/usr/bin/perl
use warnings;
use strict;
use POSIX qw(strftime);

my $curtime = time;

for (</tmp/log*>) {
my $date = (stat)[9];
my $fdate = strftime(($curtime - $date) > (365/2*24*60*60) ?
"%b %e %Y" : "%b %e %H:%M",
localtime $date);
printf "$_ $fdaten";
}
__END__

--
Benoit Izac