How to parse a text file containing this pattern “KEYWORD: Out:” and dump the result into output file using Python?
input.txt
JavaScript
x
3
1
DEBUG 2020-11:11:17.401 KEYWORD: Out:0xaaaf0000 In:0x80000000.1110ffff.
2
DEBUG 2020-11:11:17.401 KEYWORD: Out:0xaaaf00cc In:0x80000000.1110ffaa.
3
output.txt
JavaScript
1
3
1
0xaaaf0000:1110ffff
2
0x80000000:1110ffaa
3
Advertisement
Answer
You could use a regex:
JavaScript
1
12
12
1
import re
2
3
txt='''
4
DEBUG 2020-11:11:17.401 KEYWORD: Out:0xaaaf0000 In:0x80000000.1110ffff.
5
DEBUG 2020-11:11:17.401 KEYWORD: Out:0xaaaf00cc In:0x80000000.1110ffaa.'''
6
7
pat=r'KEYWORD: Out:(0x[a-f0-9]+)[ t]+In:0x[a-f0-9]+.([a-f0-9]+)'
8
9
>>> 'n'.join([m[0]+':'+m[1] for m in re.findall(pat, txt)])
10
0xaaaf0000:1110ffff
11
0xaaaf00cc:1110ffaa
12
If you want to do this line-by-line from a file:
JavaScript
1
10
10
1
import re
2
3
pat=r'KEYWORD: Out:(0x[a-f0-9]+)[ t]+In:0x[a-f0-9]+.([a-f0-9]+)'
4
5
with open(ur_file) as f:
6
for line in f:
7
m=re.search(pat, line)
8
if m:
9
print(m.group(1)+':'+m.group(2))
10