cat 1.txt
a1 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = oracle)(PORT = 1000))
(CONNECT_DATA =
(SERVER = aaa)
(SERVICE_NAME = aaa)
a2 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = bbb-vip)(PORT = 1000))
(CONNECT_DATA =
(SERVER = ccc)
(SERVICE_NAME = bbb)
a3 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = ccc-vip)(PORT = 1000))
(CONNECT_DATA =
(SERVER = ccc)
(SID = ccc)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
b = ['a1', 'a2']
with open('1.txt', 'r') as f:
a = []
lines = f.readlines()
print(lines[0])
for x in lines:
if x.startswith('a'):
a.extend([x.strip().split()[0], lines.index(x),])
for i in b[:-1]:
if i in a:
c = a.index(i)
print ''.join(lines[a[c+1]:a[c+3]])
if b[-1] == a[-2]:
print ''.join(lines[a[int(a.index(b[-1]) + 1)]:])
else:
print ''.join(lines[a[int(a.index(b[-1])) + 1]:a[int(a.index(b[-1])) + 3]])