With Python, you could use a regex and processing in a file with mode 'rb+':
import re
from time import sleep
regx = re.compile('^ +omega +omega +\[[ \d.eE+-]+\] +(\d\d?)(;.+)',re.MULTILINE|re.DOTALL)
for i in xrange(1,11):
sleep(20*60)
with open('MRFzones', 'rb+') as f:
m = regx.search(f.read())
x = m.start(1)
f.seek(x,0)
f.writelines((str(i),m.group(2)))
f.tuncate()
I suppose that the initial file is containing
' omega omega [0 0 -1 0 0 0 0] 0;'
m is the MatchObject that records the results of the matching
Among these results, there are:
- the position in the file's content where starts the 1 or 2 digit(s) sequence that is catched by the first group in the match, the one defined by
(\d\d?)
. This start is obtained via method start() of m called with 1 as argument
- the text catched by second group in the match, defined by
(;.+)
, obtained by method group() of m called with 2 as argument
re.MULTILINE makes the symbol '^' in the pattern of the regex to match with each start of line, that is precisely after each '\n'
and at the beginning of the string. Without re.MULTILINE , '^' means only the matching with the beginning of the string
re.DOTALL makes the dot in the pattern to match with any character, comprised the newlines. So ';.+'
means : the character ';' and all and every characters that follow ';' until the very end of the string. Without re.DOTALL, the symbolic dot in the pattern would stop to match at the end of the line.
After the execution of f.read()
, the file's pointer is situated at the end of the file on the hard drive. seek() allows to move it. In this case, seek(x,0)
moves the pointer to a position that is situated at x characters from the start of the file (the 0 means 'from the start', see seek() to learn the other types of moves)
After the execution of f.seek(x,0)
, the pointer is just before the integer you want to change. Writing str(i) erases and writes on the ancient number.
f.truncate()
is to make the file ending at the current position. In the case of my code, it's not absolutely necesssary , since the string that is replaced is always replaced by a string of same or longer length.
.
Note that this code can be simplified if there are two positions reserved for the number in the file where can be written two characters: ' 0', ' 1', ' 2', ' 3', etc
In this case the code can be:
import re
from time import sleep
regx = re.compile('^ +omega +omega +\[[ \d.eE+-]+\] +(\d\d?)',re.MULTILINE)
for i in xrange(1,11):
sleep(20*60)
with open('MRFzones', 'rb+') as f:
m = regx.search(f.read())
x = m.start(1)
f.seek(x,0)
f.write('{:>2}'.format(i))
or
import re
from time import sleep
regx = re.compile('^ +omega +omega +\[[ \d.eE+-]+\] +(\d\d?)(;.+)',re.MULTILINE|re.DOTALL)
with open('Copie de oso.txt', 'rb+') as f:
m = regx.search(f.read())
x = m.start(1)
f.seek(x,0)
f.writelines((' 1',m.group(2)))
f.truncate()
for i in xrange(2,11):
sleep(20)
with open('Copie de oso.txt', 'rb+') as f:
f.seek(x,0)
f.write('{:>2}'.format(i))
.
PS
Brackets '[' and ']' of the symbolic sequence [ \d.eE+-]
in the pattern define a set of characters. I put between these two brackets all the characters that are likely to be used to write a number. It is important that '-' be at the end of the set, otherwise it would mean anaother thing that just the '-' character.
'\['
and '\]'
are the escaped brackets, to represent just the characters 'brackets', not the symbolic brackets that define a set
I choosed to catch '[0 0 -1 0 0 0 0]' with the part \[[ \d.eE+-]+\]
of the pattern in case that the numbers could be any other kind of number, expressed in any sort of representation (comprised exponential notation)
' +'
in the pattern means 'any number of blanks'