I have a file that contains thousands of lines, and in the file, there are some lines like:
Line 115463: 08:59:25.106 08:59:24.992877 ASDF_IIS_CFGDB GenMod Ta-SNS__GENERATED_EVENTS (Event2f, DIR = 13) rrc_UlUtranMsg.c (../../../HEDGE/UL3/ASDF/UtranMsg/Uplink/Code/Src) 987
Line 236362: 08:59:28.647 08:59:28.597827 ASDF_IIS_CFGDB GenMod Ta-SNS__GENERATED_EVENTS (Eventab, DIR = 1) rrc_UlUtranMsg.c (../../../HEDGE/UL3/ASDF/UtranMsg/Uplink/Code/Src) 934
Line 324964: 08:59:40.456 08:59:40.403644 ASDF_IIS_CFGDB GenMod Ta-SNS__GENERATED_EVENTS (Eventac, DIR = 1) rrc_UlUtranMsg.c (../../../HEDGE/UL3/ASDF/UtranMsg/Uplink/Code/Src) 934
Line 341172: 08:59:40.659 08:59:40.616565 ASDF_IIS_CFGDB GenMod Ta-SNS__GENERATED_EVENTS (Eventfb, DIR = 13) rrc_UlUtranMsg.c (../../../HEDGE/UL3/ASDF/UtranMsg/Uplink/Code/Src) 987
Line 373186: 08:59:41.174 08:59:41.104755 ASDF_IIS_CFGDB GenMod Ta-SNS__GENERATED_EVENTS (Event2f, DIR = 1) rrc_UlUtranMsg.c (../../../HEDGE/UL3/ASDF/UtranMsg/Uplink/Code/Src) 934
Line 480217: 08:59:44.481 08:59:44.389453 ASDF_IIS_CFGDB GenMod Ta-SNS__GENERATED_EVENTS (Eventx1, DIR = 1) rrc_UlUtranMsg.c (../../../HEDGE/UL3/ASDF/UtranMsg/Uplink/Code/Src) 934
Line 505424: 08:59:44.777 08:59:44.701709 ASDF_IIS_CFGDB GenMod Ta-SNS__GENERATED_EVENTS (Event1a, DIR = 1) rrc_UlUtranMsg.c (../../../HEDGE/UL3/ASDF/UtranMsg/Uplink/Code/Src) 934
I only need to extract the substring
'1a'
from
'SNS__GENERATED_EVENTS (Event1a, DIR = 1)'
and so on. So, basically, the two characters after '(Event'
And I need to store these in a list or somewhere else where I can use them.
How can I do this?
So far, I have tried the following code but it gives me some values mixed in:
events = []
for line in input_txt_file:
if "Ta-SNS__GENERATED_EVENTS " not in line: continue
parts = line.split('Event')
event_temp = [0]
for i,part in enumerate(parts):
if part.endswith("Ta-SNS__GENERATED_EVENTS ("): event_temp[0] = parts[i+1].split(None,1)[0].split(',',2)[0]
events.append(event_temp)
print events
The output I am getting is:
[[0], [0], ['2f'], ['2f'], ['ab'], ['ab'], [0], [0], ['ac'], ['ac'], ['fb'], .......]
If the line position is always fixed, Wayne's answer is the most efficient. If the position can vary a bit, this is a decent situation in which to use regex:
This searches each line for
SNS__GENERATED_EVENTS
, followed by possibly some characters, followed byEvent
and then two more characters, and grabs those two characters.