[Fwd: Re: [Tutor] What am I missing?]

Tom Tucker tktucker at gmail.com
Mon Sep 27 07:52:06 CEST 2004


Brian,
I think I found the issue.  My "for" and "if" statements were never
reached.  The system call "logfile = os.system('tail -f
/var/log/messages')" doesn't appears to be communicating outside of
itself.  It was just printing to STDOUT.  Back to the drawing board! 
Thanks!


Tom


On Mon, 27 Sep 2004 01:36:04 -0400, Brian van den Broek
<bvande at po-box.mcgill.ca> wrote:
> Tom,
> 
> glad you felt helped!
> 
> It's a bit past my bed-time, so I've fwd'ed your reply to the tutor list
> -- if you send your replies there, you have a much better chance of
> getting help. (I'm pretty new at programming, too.)
> 
> But a couple of things:
> 
> Are you sure you need regular expressions for your task? I'm not very
> familiar with the re module, so my response didn't consider if you were
> using it correctly. But for simple searches, you can use string methods.
> (Also, rather than import string in the code below, you should use the
> string methods -- the string module, as I understand it, is still there
> for backwards compatibility, but it is not the preferred technique.)
> 
> Best,
> 
> Brian vdB
> 
> 
> 
> 
> -------- Original Message --------
> Subject: Re: [Tutor] What am I missing?
> Date: Mon, 27 Sep 2004 01:03:40 -0400
> From: Tom Tucker <tktucker at gmail.com>
> Reply-To: Tom Tucker <tktucker at gmail.com>
> To: Brian van den Broek <bvande at po-box.mcgill.ca>
> References: <2a278ffe040926205866e40963 at mail.gmail.com>
> <415797EE.1010401 at po-box.mcgill.ca>
> 
> Brain,
> Thanks for the help.  I APPRECIATE IT!
>   I think, I understand.  I have to zero out or empty the "found"
> variable.  Below is the /var/log/messages log, the Python script, and
> the script output when executed.
> 
> If you notice, my search criteria is based on the word 'CROND', my log
> (/var/log/messages) does NOT contain that word.  WIth that being said,
> when the script is excuted, technically nothing should be printed to
> STDOUT.  Correct?
> 
> /var/log/messages
> ###########
> root at loki ttucker/Python/isalog # cat /var/log/messages
> Sep 27 00:49:46 loki syslogd 1.4.1: restart.
> Sep 27 00:49:46 loki kernel: klogd 1.4.1, log source = /proc/kmsg started.
> Sep 27 00:49:46 loki kernel: Inspecting
> /boot/System.map-2.6.3-7mdk-i686-up-4GB
> Sep 27 00:49:46 loki kernel: Loaded 29391 symbols from
> /boot/System.map-2.6.3-7mdk-i686-up-4GB.
> Sep 27 00:49:46 loki kernel: Symbols match kernel version 2.6.3.
> Sep 27 00:49:46 loki kernel: No module symbols loaded - kernel modules
> not enabled.
> Sep 27 00:50:03 loki su(pam_unix)[21569]: session opened for user root
> by (uid=0)
> root at loki ttucker/Python/isalog #
> 
> Test Script (HAS CHANGED)
> ########
> import os, re, string
> 
> findme = re.compile('CROND')
> logfile = os.system('tail -f /var/log/messages')
> for line in logfile:
>          found = re.search(findme, line)
>          if found:
>                  print line,
>                  found = ''
>          else:
>                  print "not found"
>                  found = ''
> 
> SCRIPT OUTPUT
> ###############
> root at loki ttucker/Python/isalog # ./newv5.py
> Sep 27 00:49:46 loki syslogd 1.4.1: restart.
> Sep 27 00:49:46 loki kernel: klogd 1.4.1, log source = /proc/kmsg started.
> Sep 27 00:49:46 loki kernel: Inspecting
> /boot/System.map-2.6.3-7mdk-i686-up-4GB
> Sep 27 00:49:46 loki kernel: Loaded 29391 symbols from
> /boot/System.map-2.6.3-7mdk-i686-up-4GB.
> Sep 27 00:49:46 loki kernel: Symbols match kernel version 2.6.3.
> Sep 27 00:49:46 loki kernel: No module symbols loaded - kernel modules
> not enabled.
> Sep 27 00:50:03 loki su(pam_unix)[21569]: session opened for user root
> by (uid=0)
> 
> On Mon, 27 Sep 2004 00:32:46 -0400, Brian van den Broek
> <bvande at po-box.mcgill.ca> wrote:
> > Tom Tucker said unto the world upon 2004-09-26 23:58:
> >
> >
> > > Good evening! Any thoughts on this one?
> > >
> > > The goal is to 'tail -f /var/log/messages' and if the line contains
> > > "CROND" for example, then print that line.  The below script does a
> > > great job of tailing and printing, but it prints EVERYTHING.
> > >
> > > ##################
> > > #!/usr/bin/python
> > >
> > > import os, re
> > >
> > > findme = re.compile('CROND')
> > > logfile = os.system('tail -f /var/log/messages')
> > > for line in logfile:
> > >         found = re.search(findme, line)
> > >         if found:
> > >                 print line,
> > >         else:
> > >                 print "not found"
> > > ####################
> > >
> > >
> > > Thanks,
> > >
> > > Tom
> >
> > Hi Tom,
> >
> > with the line:
> >
> >  >         found = re.search(findme, line)
> >
> > you assign found a value which evaluates as True.
> >
> > Then, for each subsequent line through the for loop you are testing if
> > found evaluates as True. And it does, because you made it so. ;-)
> >
> > One fix would be to say this instead:
> >
> >         if found:
> >                 print line,
> >                 found = ''  # or [], (), None, etc. Anything that
> >                             # evaluates as False will do.
> >
> > Then, next time through, your if test will fail (unless of course your
> > re.search matched again).
> >
> > I'm not sure of your intent; if you want "not found" to be printed only
> > when you had no matches, you will need to add a bit more.
> >
> > HTH,
> >
> > Brian vdB
> >
> 
>


More information about the Tutor mailing list