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

Résolu
tonixm Messages postés 127 Statut Membre -  
tonixm Messages postés 127 Statut Membre -
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!!
Configuration: Windows XP
Internet Explorer 7.0

36 réponses

  • 1
  • 2
  1. tonixm Messages postés 127 Statut Membre 4
     
    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
  2. tonixm Messages postés 127 Statut Membre 4
     
    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
  3. lami20j Messages postés 21506 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 571
     
    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
  4. tonixm Messages postés 127 Statut Membre 4
     
    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
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. lami20j Messages postés 21506 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 571
     
    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
  7. tonixm Messages postés 127 Statut Membre 4
     
    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
  8. lami20j Messages postés 21506 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 571
     
    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
  9. tonixm Messages postés 127 Statut Membre 4
     
    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
  10. lami20j Messages postés 21506 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 571
     
    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
  11. tonixm Messages postés 127 Statut Membre 4
     
    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
  12. lami20j Messages postés 21506 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 571
     
    je vais voir ce soir à la maison
    0
  13. tonixm Messages postés 127 Statut Membre 4
     
    d'accord merci
    0
  14. tonixm Messages postés 127 Statut Membre 4
     
    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
  15. tonixm Messages postés 127 Statut Membre 4
     
    c'est pour sa que je trie et fait une substitution!
    0
  16. tonixm Messages postés 127 Statut Membre 4
     
    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
  17. lami20j Messages postés 21506 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 571
     
    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
  18. tonixm Messages postés 127 Statut Membre 4
     
    merci pour tout!!!
    j ai le compilateur au boulot et j'essaierai sa demain au plsu vite!
    j espere que cela marchera
    0
  19. tonixm Messages postés 127 Statut Membre 4
     
    et comment faire pour mettre le resultat dans un fichier .analyse?
    0
  20. lami20j Messages postés 21506 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 571
     
    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
  21. tonixm Messages postés 127 Statut Membre 4
     
    merci pour le temps passé à m'aider!!!!
    je vais essayer cela demain!!!
    0
  • 1
  • 2