1

I am working on a short script which tails a logfile. it should be possible to filter the output with regex and split by /t if necessary.

I wrote something like this in perl some years ago but have to change it to python

perl-code-sniplet:

open(FILE,"tail -F logfile | grep -v something | ");
while(<FILE>){
    my @lineparts = split(/\t/,$_);
    ...
    do something else
    ...
}

I've already tried Popen different varieties (logger.sh contains tail -F on an remote server via ssh)

cmd = subprocess.Popen("./logger.sh").communicate()
for line in cmd.stdout.readline(): 
    test = line

    if re.match('.*ERR.*', test):
        print "ERROR"

But all I get is an output on stdout and I cannot manipulate it (never printed "ERROR")

Anyone got any ideas?

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
Peter Sander
  • 15
  • 1
  • 4

1 Answers1

2

I can't help with the regex because you did not post an example of how the log file looks like. I can help with the tail of the file, since that's quite universal.

data = []
with open(logfile,'rt',encoding='utf-8')as infile:
    for i,e in enumerate(infile):
        data.append(e.strip())
    if i < 11:
        for line in data:
            print(line)
    else:
        for line in data[:-10]:
            print(line)

In your case you would save the "print" into a container and then run your regex against it.

SgtMajorJay
  • 129
  • 4