I have below working code which works fine, but just looking way around if there is an another elegant way of doing this, because here i'm using else condition to print the last line or dataBlock to be printed.
Note: mydudalt1
and mydudalt2
are the host names
My data File:
$ cat mydata_test.txt
-- mydudalt1 --
192.168.2.40; // udalt-sanjose
192.168.2.56; // udalt-sj1
192.168.98.71; // udalt-japan
192.168.2.146; //udalt-sj-fwd1
-- mydudalt2 --
199.43.4.70; // udalt-chelms
192.168.2.56; // udalt-sj1
192.168.98.71; // udalt-japan
My Working code:
#!/grid/common/pkgs/python/v3.6.1/bin/python3
dataBlock = ''
with open('mydata_test.txt', 'r') as frb:
for line in frb:
line = line.strip("\n")
if line.startswith('--'):
if "japan" in dataBlock:
print(dataBlock)
dataBlock = ''
dataBlock = dataBlock + line
elif "japan" in line:
dataBlock = dataBlock + line
else:
print(dataBlock + line)
Resulted Output:
-- mydudalt1 -- 192.168.98.71; // udalt-japan
-- mydudalt2 -- 192.168.98.71; // udalt-japan
mydudalt
blocks have more than one JAPAN Ids in them? \$\endgroup\$mydudalt1
andmydudalt2
are the host names. \$\endgroup\$