[PERL]- Extraire lignes d'un fichier à partir

Résolu/Fermé
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 - 9 avril 2008 à 11:54
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 - 17 avril 2008 à 16:41
Bonjour,

je suis nouveau sur ce forum et je dois pour mon stage extraire les "nb" derinères lignes d'un fichier, les trier et les mettre dans un autre fichier.

En gros je voudrais faire cela en perl:
tail -$nb $fichier | sort -k 5 -u | sed -e 's/\[[0-9]*\]:/:/g' > $file.analyse

Au secours!!
A voir également:

36 réponses

tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 21:25
pas exactement ce que je voulais

je veux traiter juste les "nb_line" dernières lignes du fichier n'importe lesquelles qu'elles soient.

Par exemple pour tonixm.log :lami20j@debian:~$ cat tonixm.log
Apr 9 15:58:32 vmonitor ntpd[31608]: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 EiDT 2006 (1)
Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 17.000 usec
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, 0.0.0.0#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, ::#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface lo, 127.0.0.1#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface eth0, 172.16.15.39#123
Apr 9 15:58:32 vmonitor ntpd[31608]: kernel time sync status 0040
Apr 9 15:58:32 vmonitor ntpd[31608]: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 EiDT 2006 (1)
Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 14.000 usec
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, 0.0.0.0#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, ::#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface lo, 127.0.0.1#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface eth0, 172.16.15.39#123
Apr 9 15:58:32 vmonitor ntpd[31608]: kernel time sync status 0040
Apr 9 15:58:32 vmonitor ntpd[31608]: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 EiDT 2006 (1)
Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 13.000 usec
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, 0.0.0.0#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, ::#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface lo, 127.0.0.1#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface eth0, 172.16.15.39#123
Apr 9 15:58:32 vmonitor ntpd[31608]: kernel time sync status 0040
Apr 9 15:58:32 vmonitor ntpd[31608]: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 EiDT 2006 (1)
Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 18.000 usec
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, 0.0.0.0#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, ::#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface lo, 127.0.0.1#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface eth0, 172.16.15.39#123
Apr 9 15:58:32 vmonitor ntpd[31608]: kernel time sync status 0040
Apr 9 15:58:32 vmonitor ntpd[31608]: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 EiDT 2006 (1)
Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 10.000 usec
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, 0.0.0.0#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, ::#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface lo, 127.0.0.1#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface eth0, 172.16.15.39#123
Apr 9 15:58:32 vmonitor ntpd[31608]: kernel time sync status 0040


si nb_line vaut 4.

je veux au final dans le .sort que des lignes de ce type:

vmonitor ntpd: Listening on interface wildcard, ::#123
vmonitor ntpd: Listening on interface lo, 127.0.0.1#123
vmonitor ntpd: Listening on interface eth0, 172.16.15.39#123
vmonitor ntpd: kernel time sync status 0040

C'est a dire prendre les 4 dernières lignes, enlever "Apr 9 15:58:32" et [31608] puis tout mettre dans file.sort
1
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 12:00
J'ai essayer ceci :


open (FIC,"$file");
my $line;
$line=0;
while (<FIC>){
# par défaut chaque ligne lue (y compris le délimiteur
# de fin de ligne) est stockée dans $_
if($line >= $nb_line){
$_=~ s/\[[0-9]*\]:/:/g;
system("cat $_ > $file_analyse");
}
$line++;
}
close (FIC);


Mais sans succès.
0
lami20j Messages postés 21331 Date d'inscription jeudi 4 novembre 2004 Statut Modérateur, Contributeur sécurité Dernière intervention 30 octobre 2019 3 567
9 avril 2008 à 12:32
Salut,

mets ton fichier sur ci-joint.com
je vais regarder

ta demande n'est pas claire

si je comprends bien tu veux
1. traiter les n dernières lignes dans fichies
2. trier les lignes

Je vois aussi que tu fait une substitution.

Veux-tu être plus explicite?
Merci.
0
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 13:22
Salut,

j'ai un fichier de log qu'il faut que je trie. J'ai mis un index "nbline" afin de ne pas retraiter a chaque fois ce que j'ai deja fait.
Il faut que:
1. traiter les n dernières lignes de mon fichier
2. trier les lignes
3. substituer certains caractères
4. remettre tout cela dans un autre fichier X.analyse

Mon tuteur m'a dit qu'il fallais "simplement" que je traduise cela en perl:
tail -$nb $fichier | sort -k 5 -u | sed -e 's/\[[0-9]*\]:/:/g' > $file.analyse

merci
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
lami20j Messages postés 21331 Date d'inscription jeudi 4 novembre 2004 Statut Modérateur, Contributeur sécurité Dernière intervention 30 octobre 2019 3 567
9 avril 2008 à 15:13
Re,

ce qui t'as demandé ton tuteur c'est ton problème

pour t'aider j'ai besoi de ton fichier
pour faire la traduction, il faut que je comprenne le problème
c'est comme ça que je travail moi ;-)

donc j'attends ton fichier pour faire des test
merci
0
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 15:26
c'est un fichier confidentiel et je ne peux pas te le mettre sur cijoint.com ou .fr je ne me rappel plus sinon je l'aurais fait depuis lontemps.
merci quand même
0
lami20j Messages postés 21331 Date d'inscription jeudi 4 novembre 2004 Statut Modérateur, Contributeur sécurité Dernière intervention 30 octobre 2019 3 567
9 avril 2008 à 15:29
Je n'ai pas besoin de connaître le contenu de ton fichier
En revanche j'ai besoin de la structure de ton fichier (tu n'as qu'à mettre des données bidons ;-))
0
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 15:35
LOG_DIR="/var/adm/syslog.dated/current"
CONF_DIR="/doi/lib"

for file in $(echo "daemon.log kern.log") ; do

FILE=$LOG_DIR/$file
if [ ! -e $FILE ]; then
echo "Error EXP039" >> $FILE.focal
exit 1
fi

CONF_FILE=$CONF_DIR/$file.cfg
if [ ! -e $CONF_FILE ]; then
echo "Error EXP039" >> $FILE.focal
exit 1
fi

PREVIOUS_FILE=$(echo $FILE | sed -e 's/current/previous/')
CURRENT_FILE_LENGHT=$(($(cat $FILE | wc -l)))

if [ ! -e $FILE.focal.end ] ; then
# Si le fichier de log n'a jamais ete parse,
# ne pas oublier de parser le fichier de log de la veille !
if [ ! -e $PREVIOUS_FILE.focal.end ] ; then
# Si le fichier de log de la veille n'a jamais ete parse,
# on a un pb ! => erreur focal !! dans le fichier du jour !
echo "Error EXP040" >> $FILE.focal
else
# Sinon, on parse le fichier de la veille
PREVIOUS_FILE_LENGHT=$(($(cat $PREVIOUS_FILE | wc -l)))
. $PREVIOUS_FILE.focal.end
NB_LINE=$(($PREVIOUS_FILE_LENGHT-$LAST_LINE))

# On redirige les logs dans un fichier temporaire
tail -$NB_LINE $PREVIOUS_FILE | sort -k 5 -u | sed -e 's/\[[
0-9]*\]:/:/g' > $FILE.sort

# On exclue les messages qui ne nous interessent pas
cat $CONF_FILE | while read line
do
cat $FILE.sort | grep -v "$line" > $FILE.tmp
cat $FILE.tmp > $FILE.sort
done

# Sans oublier de rediriger les logs dans le fichier du jour
!
# en les remettant dans l'ordre.
cat $FILE.sort | sort >> $FILE.focal
KEEP_LENGHT=$(($(cat $FILE.sort | wc -l)))
rm -rf $FILE.sort
rm -rf $FILE.tmp

echo "LAST_LINE=$PREVIOUS_FILE_LENGHT" > $PREVIOUS_FILE.foca
l.end
tracer "Keeping $KEEP_LENGHT/$NB_LINE line(s) of $PREVIOUS_F
ILE"

fi

# Une fois les logs de la veille traites, on s'occuppe des logs
du jour.
NB_LINE=$(($CURRENT_FILE_LENGHT))
else
# On reprend la ou on en etait
. $FILE.focal.end
NB_LINE=$(($CURRENT_FILE_LENGHT-$LAST_LINE))
fi

# On redirige les logs dans un fichier temporaire
tail -$NB_LINE $FILE | sort -k 5 -u | sed -e 's/\[[0-9]*\]:/:/g' > $
FILE.sort


# On exclue les messages qui ne nous interessent pas
cat $CONF_FILE | while read line
do
cat $FILE.sort | grep -v "$line" > $FILE.tmp
cat $FILE.tmp > $FILE.sort
done

# Sans oublier de rediriger les logs dans le fichier du jour !
# en les remettant dans l'ordre.
cat $FILE.sort | sort >> $FILE.focal
KEEP_LENGHT=$(($(cat $FILE.sort | wc -l)))
rm -rf $FILE.sort
rm -rf $FILE.tmp

echo "LAST_LINE=$CURRENT_FILE_LENGHT" > $FILE.focal.end
tracer "Keeping $KEEP_LENGHT/$NB_LINE line(s) of $FILE"
0
lami20j Messages postés 21331 Date d'inscription jeudi 4 novembre 2004 Statut Modérateur, Contributeur sécurité Dernière intervention 30 octobre 2019 3 567
9 avril 2008 à 15:55
Bon,

je vois qu'on ne se comprends pas

ce n'est pas le script que j'ai demandé
j'ai demandé un fichier que tu traites (donc un fichier log)
0
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 16:02
ha dsl:

daemon.log:

Apr 9 15:58:32 vmonitor ntpd[31608]: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 E
DT 2006 (1)
Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 17.000 usec
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, 0.0.0.0#1
23
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, ::#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface lo, 127.0.0.1#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface eth0, 172.16.15.39#
123
Apr 9 15:58:32 vmonitor ntpd[31608]: kernel time sync status 0040
0
lami20j Messages postés 21331 Date d'inscription jeudi 4 novembre 2004 Statut Modérateur, Contributeur sécurité Dernière intervention 30 octobre 2019 3 567
9 avril 2008 à 16:26
je vais voir ce soir à la maison
0
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 16:28
d'accord merci
0
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 16:57
en fait on voudrait garder par exemple pour:

Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 17.000 usec

pour cette ligne on voudrait :

vmonitor ntpd: precision = 17.000 usec
0
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 19:41
c'est pour sa que je trie et fait une substitution!
0
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 20:41
il faut prendre les "nb-line" dernières lignes d'un fichier X.sort, faire un sort -k 5 -u (je ne sais pas ce que cela veut dire) puis enlevé les chiffres avant ":" par substitution.
finalement mettre tout cela dans un fichier X.analyse
aie aie je suis mal en point
0
lami20j Messages postés 21331 Date d'inscription jeudi 4 novembre 2004 Statut Modérateur, Contributeur sécurité Dernière intervention 30 octobre 2019 3 567
9 avril 2008 à 21:47
Alors essaie celui là
#!/usr/bin/perl
use strict;use warnings;

open LIRE,"tonixm.log"
     or die "E/S : $!\n";
print "Entrez le nombre de lignes : ";
chomp(my $n = <STDIN>);

my @log = <LIRE>;

grep {s/.*(vmonitor\s*ntpd)\[.*\](:.*)/${1}$2/} @log;
print map  {$_->[0]}
      sort {$a->[1] cmp $b->[1]}
      map  {[$_,(split / /)[4]]}
      splice(@log,-$n);

__END__

0
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 21:49
merci pour tout!!!
j ai le compilateur au boulot et j'essaierai sa demain au plsu vite!
j espere que cela marchera
0
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 21:52
et comment faire pour mettre le resultat dans un fichier .analyse?
0
lami20j Messages postés 21331 Date d'inscription jeudi 4 novembre 2004 Statut Modérateur, Contributeur sécurité Dernière intervention 30 octobre 2019 3 567
9 avril 2008 à 22:01
lami20j@debian:~$ cat log.pl
#!/usr/bin/perl
use strict;use warnings;

open LIRE,"tonixm.log"
     or die "E/S : $!\n";

open ECRIRE,">fichier.analyse"
     or die "E/S : $!\n";

print "Entrez le nombre de lignes : ";
chomp(my $n = <STDIN>);

my @log = <LIRE>;

grep {s/.*(vmonitor\s*ntpd)\[.*\](:.*)/${1}$2/} @log;
print ECRIRE map  {$_->[0]}
             sort {$a->[1] cmp $b->[1]}
             map  {[$_,(split / /)[4]]}
             splice(@log,-$n);

__END__
lami20j@debian:~$ cat tonixm.log
Apr 9 15:58:32 vmonitor ntpd[31608]: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 EiDT 2006 (1)
Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 17.000 usec
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, 0.0.0.0#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, ::#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface lo, 127.0.0.1#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface eth0, 172.16.15.39#123
Apr 9 15:58:32 vmonitor ntpd[31608]: kernel time sync status 0040
Apr 9 15:58:32 vmonitor ntpd[31608]: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 EiDT 2006 (1)
Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 14.000 usec
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, 0.0.0.0#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, ::#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface lo, 127.0.0.1#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface eth0, 172.16.15.39#123
Apr 9 15:58:32 vmonitor ntpd[31608]: kernel time sync status 0040
Apr 9 15:58:32 vmonitor ntpd[31608]: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 EiDT 2006 (1)
Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 13.000 usec
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, 0.0.0.0#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, ::#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface lo, 127.0.0.1#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface eth0, 172.16.15.39#123
Apr 9 15:58:32 vmonitor ntpd[31608]: kernel time sync status 0040
Apr 9 15:58:32 vmonitor ntpd[31608]: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 EiDT 2006 (1)
Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 18.000 usec
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, 0.0.0.0#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, ::#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface lo, 127.0.0.1#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface eth0, 172.16.15.39#123
Apr 9 15:58:32 vmonitor ntpd[31608]: kernel time sync status 0040
Apr 9 15:58:32 vmonitor ntpd[31608]: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 EiDT 2006 (1)
Apr 9 15:58:32 vmonitor ntpd[31608]: precision = 10.000 usec
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, 0.0.0.0#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface wildcard, ::#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface lo, 127.0.0.1#123
Apr 9 15:58:32 vmonitor ntpd[31608]: Listening on interface eth0, 172.16.15.39#123
Apr 9 15:58:32 vmonitor ntpd[31608]: kernel time sync status 0040
lami20j@debian:~$ perl log.pl
Entrez le nombre de lignes : 10
lami20j@debian:~$ cat fichier.analyse
vmonitor ntpd: precision = 10.000 usec
vmonitor ntpd: ntpd 4.2.0a@1.1190-r Thu Oct 5 04:11:32 EiDT 2006 (1)
vmonitor ntpd: Listening on interface lo, 127.0.0.1#123
vmonitor ntpd: Listening on interface eth0, 172.16.15.39#123
vmonitor ntpd: Listening on interface wildcard, 0.0.0.0#123
vmonitor ntpd: Listening on interface wildcard, ::#123
vmonitor ntpd: Listening on interface lo, 127.0.0.1#123
vmonitor ntpd: Listening on interface eth0, 172.16.15.39#123
vmonitor ntpd: kernel time sync status 0040
vmonitor ntpd: kernel time sync status 0040
lami20j@debian:~$
0
tonixm Messages postés 127 Date d'inscription mercredi 9 avril 2008 Statut Membre Dernière intervention 30 janvier 2009 4
9 avril 2008 à 22:08
merci pour le temps passé à m'aider!!!!
je vais essayer cela demain!!!
0