Lire des fichier dans un repertoire en perl
Kadavra
-
lami20j Messages postés 21506 Date d'inscription Statut Modérateur, Contributeur sécurité Dernière intervention -
lami20j Messages postés 21506 Date d'inscription Statut Modérateur, Contributeur sécurité Dernière intervention -
Bonjour,
j'aimerais savoir comment faire pour lire le nombre de lignes de plusieurs fichier dans un répertoire en Perl.
voici mon code:
#!/usr/bin/perl
opendir REP, "test" or die "impossible d'ouvrir le repertoire";
open(FICHIER,"<Lettre.txt") || die "Problème à l\'ouverture : $!";
open (WRITER,">> TraceNbLigne.txt") or die "Le fichier ne peut etre édité ! \n";
$i = 0;
while(<FICHIER>)
{
$i ++;
}
print WRITER "Il y a $i lignes \n";
print "\nNombre de lignes : $i\n";
close FICHIER || die "Problème à la fermeture : $!";
close Rep ;
close WRITER
quand je lit seulement le fichier.txt qui est sur mon bureau, j'arrive a relever le nombre de lignes, et a le tracer dans un fichier.txt, mais quand je place mon fichier dans un repertoire, je n'arrive pas a aller le lire, si quelqu'un peut m'aider, merci d'avance
j'aimerais savoir comment faire pour lire le nombre de lignes de plusieurs fichier dans un répertoire en Perl.
voici mon code:
#!/usr/bin/perl
opendir REP, "test" or die "impossible d'ouvrir le repertoire";
open(FICHIER,"<Lettre.txt") || die "Problème à l\'ouverture : $!";
open (WRITER,">> TraceNbLigne.txt") or die "Le fichier ne peut etre édité ! \n";
$i = 0;
while(<FICHIER>)
{
$i ++;
}
print WRITER "Il y a $i lignes \n";
print "\nNombre de lignes : $i\n";
close FICHIER || die "Problème à la fermeture : $!";
close Rep ;
close WRITER
quand je lit seulement le fichier.txt qui est sur mon bureau, j'arrive a relever le nombre de lignes, et a le tracer dans un fichier.txt, mais quand je place mon fichier dans un repertoire, je n'arrive pas a aller le lire, si quelqu'un peut m'aider, merci d'avance
Configuration: Windows Vista / Firefox 3.5.8
1 réponse
-
Salut,
Voici un script qui lit tous les fichiers .txt d'un répertoire et il affiche le nom de fichier (arborescence complète) et le nombre de lignes de chaque fichier.#!/usr/bin/perl use strict;use warnings; my %h; my $rep = "/home/lami20j"; opendir(REP,$rep) or die "E/S : $!\n"; while(defined(my $fic=readdir REP)){ my $f="${rep}/$fic"; if($fic=~/.*\.txt/){ open FIC, "$f" or warn "$f E/S: $!\n"; while(<FIC>){} $h{$f}=$.; close FIC; } } printf "%-35s - $h{$_} ligne(s)\n",$_ for sort keys %h; __END__Le résultatlami20j@debian:~$ perl nombrelignes.pl /home/lami20j/a.txt - 5 ligne(s) /home/lami20j/aa.txt - 1 ligne(s) /home/lami20j/adresses.txt - 2 ligne(s) /home/lami20j/awk.txt - 3 ligne(s) /home/lami20j/b.txt - 5 ligne(s) /home/lami20j/c.txt - 5 ligne(s) /home/lami20j/cp.txt - 15 ligne(s) /home/lami20j/dtd.txt - 10 ligne(s) /home/lami20j/ecrire.txt - 3 ligne(s) /home/lami20j/fdisk.txt - 15 ligne(s) /home/lami20j/fic.txt - 3 ligne(s) /home/lami20j/fic1.txt - 5 ligne(s) /home/lami20j/fic2.txt - 7 ligne(s) /home/lami20j/fich.txt - 2 ligne(s) /home/lami20j/fichier.txt - 3 ligne(s) /home/lami20j/fichier1.txt - 1 ligne(s) /home/lami20j/fichier2.txt - 11 ligne(s) /home/lami20j/fichier3.txt - 1 ligne(s) /home/lami20j/fichier4.txt - 1 ligne(s) /home/lami20j/fichier5.txt - 1 ligne(s) /home/lami20j/fichier_1.txt - 3 ligne(s) /home/lami20j/fichier_2.txt - 3 ligne(s) /home/lami20j/fichier_3.txt - 3 ligne(s) /home/lami20j/fichier_4.txt - 3 ligne(s) /home/lami20j/fichier_resultat.txt - 0 ligne(s) /home/lami20j/fichier_resultat2.txt - 17 ligne(s) /home/lami20j/fjoint.txt - 0 ligne(s) /home/lami20j/fr.txt.bak - 5 ligne(s) /home/lami20j/h.txt - 1825 ligne(s) /home/lami20j/hexa.txt - 103 ligne(s) /home/lami20j/iptables.start.txt - 31 ligne(s) /home/lami20j/iptables.stop.txt - 15 ligne(s) /home/lami20j/iptables.txt - 13 ligne(s) /home/lami20j/ligne.txt - 8 ligne(s) /home/lami20j/ligne0.txt - 7 ligne(s) /home/lami20j/lire.txt - 1 ligne(s) /home/lami20j/mail.txt - 6 ligne(s) /home/lami20j/monExemple.txt - 4 ligne(s) /home/lami20j/number.txt - 4 ligne(s) /home/lami20j/pagination.txt - 199 ligne(s) /home/lami20j/plop.txt - 14 ligne(s) /home/lami20j/regex.txt - 2 ligne(s) /home/lami20j/resultat.txt - 4 ligne(s) /home/lami20j/robot.txt - 4 ligne(s) /home/lami20j/sedaccents_html.txt - 5 ligne(s) /home/lami20j/t.txt - 10 ligne(s) /home/lami20j/target.txt - 6 ligne(s) /home/lami20j/text.txt - 18 ligne(s) /home/lami20j/texte.txt - 4 ligne(s) /home/lami20j/topic.txt - 66128 ligne(s) /home/lami20j/topic.txt.zip - 4310 ligne(s) /home/lami20j/topic_1.txt - 137676 ligne(s) /home/lami20j/tt.txt - 5 ligne(s) /home/lami20j/whatis.txt - 8548 ligne(s)