I have text file which is made up with several sections. Sections always started with non-space. And sub-section always started with space. Based on input.txt, following is my expected result. in this example i am trying to search for “101” and if 101 appears either in section or subsection. i want to display section with subsection. i am trying to parse the section and store in dynamic variable. But i am not sure how to store section dynamically in a variable.
Input.txt
test1 text101
aaa
bbb
ccc
test2 text101
aaa
bbb
ccc
ddd 101
test3 text101 - 123
test4 text123
aaa
bbb
ccc
ddd 101
test5 text456
aaa
bbb
ccc
test6 101
qqq
ppp
test7 text101 - 123
test8 text102 - 123
Test9 text101 - 123
Test10 text102 - 123
Python 3.0 code:
find_txt = '101'
result = []
f = open(r'\\input.txt')
for line in f:
if (line[:1]!=' '):
result.append(line)
print ('Result:')
for element in result:
if find_txt in element:
print (element, end='')
Output:
test1 text101
aaa
bbb
ccc
test2 text101
aaa
bbb
ccc
ddd 101
test3 text101 - 123
test4 text123
aaa
bbb
ccc
ddd 101
test6 101
qqq
ppp
test7 text101 - 123
Test9 text101 - 123