Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1411 connectés 

  FORUM HardWare.fr
  Programmation
  Perl

  probleme d ecriture dans un fichier

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

probleme d ecriture dans un fichier

n°1205157
CyBerNetX
Posté le 22-09-2005 à 15:10:05  profilanswer
 

Bonjour
 
mon script permet de faire un tail -f fichier.log |grep patern >ouput.log
sauf que la redirection vers output.log ne passe pas alors j ai reecrit la roue
un script qui fait un tail -f (repris sur le net)
ajout du critere de recherche de patern
et ecriture du resultat dans un fichier et c'est la ou je ne comprend plus
mon fichier ce cree mais reste vide meme quand le regex est bon cad qu il m afiche la ligne comme dans tail mais pas dans mon fichier
 
voici mon code:

Code :
  1. #!/usr/bin/perl -w
  2. #A Perl version of the unix (tail -f) command
  3. #written by Nick Pinckernell (see http://illx.org for more details)
  4. #uses the 'trailing a growing file' from Perl Cookbook
  5. #NOTE: This script DOES NOT print the last 10 lines of the
  6. #file like tail does... it just reads a growing file
  7. #####################################################
  8. #
  9. # modify:  script is a "tail -f log|grep partern >ouput.log" command
  10. #
  11. #
  12. #
  13. sub help{
  14. printf <<__HELP__;
  15. Usage $0 [Patern] [Log a parser] [Log de destination]
  16. __HELP__
  17. }
  18. my $patern = $ARGV[0];
  19. unless($patern) {
  20.   help();
  21.   exit 1;
  22. }
  23. my $log = $ARGV[1];
  24. unless($log) {
  25.     help();
  26.     exit 1;
  27. }
  28. my $tmpfile = $ARGV[2];
  29. unless($tmpfile) {
  30.     help();
  31.     exit 1;
  32. }
  33. unless( -e $log ) {
  34.     print "The logfile $log you specified doesn't exist, exiting...\n";
  35.     exit 1;
  36. }
  37. use IO::Handle;
  38. open(LOG, "$log" ) or die "Can't open $log: $!\n";
  39. open(TMPFILE,">$tmpfile" ) or die "Can't open $tmpfile: $!\n";
  40. #seek to end of file
  41. seek(LOG, 0, 2);
  42. #and start watching it from there
  43. for(;;) {
  44.     while(<LOG> ) {
  45.         if (/$patern/) {
  46.            print "$_";
  47.            print TMPFILE "$_";
  48.         }
  49.     }
  50.     #change sleep for faster or slower updating
  51.     sleep 1;
  52.     LOG->clearerr();
  53. }
  54. close(LOG);
  55. close(TMPFILE);


 
si quelqu un a une idée
a oui aussi je doit faire arreter le tail au bout d un certain delay
ca doit ce mettre quelque part dans ma boucle for mais je vois pas comment
 
merci a vous

Message cité 1 fois
Message édité par CyBerNetX le 22-09-2005 à 15:49:56
mood
Publicité
Posté le 22-09-2005 à 15:10:05  profilanswer
 

n°1205369
CyBerNetX
Posté le 22-09-2005 à 17:40:32  profilanswer
 

bon jai trouver pour la creation du fichier ligne 56 les guillemet en trop

Code :
  1. print TMPFILE $_;


pour le delay j ai modifié for

Code :
  1. for(1..60) {


mais il ne parse pas tous j ai l impression
peut etre du au delay trop long
si vous avez des idées merci :)  
 

CyBerNetX a écrit :

Bonjour
 
mon script permet de faire un tail -f fichier.log |grep patern >ouput.log
sauf que la redirection vers output.log ne passe pas alors j ai reecrit la roue
un script qui fait un tail -f (repris sur le net)
ajout du critere de recherche de patern
et ecriture du resultat dans un fichier et c'est la ou je ne comprend plus
mon fichier ce cree mais reste vide meme quand le regex est bon cad qu il m afiche la ligne comme dans tail mais pas dans mon fichier
 
voici mon code:

Code :
  1. #!/usr/bin/perl -w
  2. #A Perl version of the unix (tail -f) command
  3. #written by Nick Pinckernell (see http://illx.org for more details)
  4. #uses the 'trailing a growing file' from Perl Cookbook
  5. #NOTE: This script DOES NOT print the last 10 lines of the
  6. #file like tail does... it just reads a growing file
  7. #####################################################
  8. #
  9. # modify:  script is a "tail -f log|grep partern >ouput.log" command
  10. #
  11. #
  12. #
  13. sub help{
  14. printf <<__HELP__;
  15. Usage $0 [Patern] [Log a parser] [Log de destination]
  16. __HELP__
  17. }
  18. my $patern = $ARGV[0];
  19. unless($patern) {
  20.   help();
  21.   exit 1;
  22. }
  23. my $log = $ARGV[1];
  24. unless($log) {
  25.     help();
  26.     exit 1;
  27. }
  28. my $tmpfile = $ARGV[2];
  29. unless($tmpfile) {
  30.     help();
  31.     exit 1;
  32. }
  33. unless( -e $log ) {
  34.     print "The logfile $log you specified doesn't exist, exiting...\n";
  35.     exit 1;
  36. }
  37. use IO::Handle;
  38. open(LOG, "$log" ) or die "Can't open $log: $!\n";
  39. open(TMPFILE,">$tmpfile" ) or die "Can't open $tmpfile: $!\n";
  40. #seek to end of file
  41. seek(LOG, 0, 2);
  42. #and start watching it from there
  43. for(1..60) {
  44.     while(<LOG> ) {
  45.         if (/$patern/) {
  46.            print "$_";
  47.            print TMPFILE $_;
  48.         }
  49.     }
  50.     #change sleep for faster or slower updating
  51.     sleep 1;
  52.     LOG->clearerr();
  53. }
  54. close(LOG);
  55. close(TMPFILE);


 
si quelqu un a une idée
a oui aussi je doit faire arreter le tail au bout d un certain delay
ca doit ce mettre quelque part dans ma boucle for mais je vois pas comment
 
merci a vous



Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Perl

  probleme d ecriture dans un fichier

 

Sujets relatifs
Probleme d'écriture dans un fichier[c/c++] petit probleme d'ecriture dans un fichier
petit probleme d'ecriture dans un fichierproblème pour ecriture de résultat dans un fichier text
probleme d ecriture dans un fichierprobleme ecriture fichier
Problème de noob ecriture/lecture fichier texteProblème écriture dans fichier texte
Ecriture dans un fichier XLS : problème de format[C++ Builder] Problème étrange entre OpenDialog & écriture de fichier
Plus de sujets relatifs à : probleme d ecriture dans un fichier


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR