PythonSed
PythonSed copied to clipboard
If pythonsed is used in a loop rotating through file with each file it repeats the contents for how many times you are going through the loop
I am trying to use pythonsed to extract paragraphs of text from multiple log files.
The output for the second and subsequest file is repeated for each file. first file is normal second file repeats the data twice third file repeats the data three times.
Sample code to reproduce this.
from PythonSed import Sed, SedException
sed = Sed()
files=['file1.txt','file2.txt','file3.txt']
for file in files:
with open(file, "w") as f:
f.write('this is test'+str(files.index(file))+' of three\n')
for file in files:
try:
sed.no_autoprint = True
sed.regexp_extended = False
sed.load_string('/test/,/three/p')
sed.apply(file)
except SedException as e:
print(e.message)
except:
raise
OUTPUT IS: this is test0 of three this is test1 of three this is test1 of three this is test2 of three this is test2 of three this is test2 of three
If I use a variable to capture the output I get the same symptom.
from PythonSed import Sed, SedException
sed = Sed()
files=['file1.txt','file2.txt','file3.txt']
for file in files:
with open(file, "w") as f:
f.write('this is test'+str(files.index(file))+' of three\n')
for file in files:
try:
sed.no_autoprint = True
sed.regexp_extended = False
sed.load_string('/test/,/three/p')
mytest=sed.apply(file,output=None)
except SedException as e:
print(e.message)
except:
raise
print(mytest)
OUTPUT: ['this is test0 of three\n'] ['this is test1 of three\n', 'this is test1 of three\n'] ['this is test2 of three\n', 'this is test2 of three\n', 'this is test2 of three\n']
Am I doing something wrong?
Thanks Rob