Reputation: 82
I'm using a home tool that parses the computer configuration to verify if some basic configuration is applied or not and if not, it generates an alert in a textfile on the host on which I run the tool.
The tool don't make a file by computer that don't work but a file for all.
I'd like to parse this text file and get each paragraphs corresponding to each computer to send an email to IT whose in charge of the computer to tell him what he has to do.
For example below:
---- mycomputerone ----
Hello
During Test of mycomputerone following misconfiguration were detected
- bad ip adress
- bad name
please could take the action to correct it and come back to us?
---- mycomputertwo ----
Hello
During Test of mycomputertwo following misconfiguration were detected
- bad ip adress
- bad name
- administrative share available
please could take the action to correct it and come back to us?
---- mycomputerthree ----
.....
I'd like to get the text between hello
and ?
but can't manage how to do it
I tried
re.search(r'hello'(S*\w+)\?'), text)
It didn't work. I read the file via
d = open(file, 'r'; encoding="UTF-8")
text = d.read()
Upvotes: 0
Views: 1046
Reputation: 626926
What you ask for is
re.findall(r'(?m)^\s*Hello\s*[^?]+', d)
where d
is the whole file read in as a single string. See this demo. It is a bit vulnerable since it won't work correctly if the contents contains ?
.
I suggest reading line by line, checking if a line starts with ---
and then add the subsequent lines to a current record.
See the following Python demo:
items = []
tmp = ''
with open(file, 'r'; encoding="UTF-8") as d:
for line in d:
if (line.strip().startswith('---')):
if tmp:
items.append(tmp.strip())
tmp = ''
else:
tmp = tmp + line + "\n"
if tmp:
items.append(tmp)
print(items)
Output:
['Hello\n\n During Test of mycomputerone following misconfiguration were detected\n - bad ip adress\n - bad name\n\n please could take the action to correct it and come back to us?',
'Hello\n\n During Test of mycomputertwo following misconfiguration were detected\n - bad ip adress\n - bad name\n - administrative share available\n\n please could take the action to correct it and come back to us?']
Upvotes: 1