Here is the data that i am trying to manipulate.
start of show feature
Feature Name Instance State
bash-shell 1 disabled
bfd 1 disabled
bgp 1 disabled
end of show feature
I am trying to get data between 2 lines.
Here is a powershell example
$from = ($switchinfo | Select-String -pattern "start of show feature" | Select-Object LineNumber).LineNumber
$to = ($switchinfo | Select-String -pattern "end of show feature" | Select-Object LineNumber).LineNumber -gt $from
$a = $switchinfo | where {$_.readcount -gt $from -and $_.readcount -lt $to}
So far I have this for python 2.7.12
fromStr = 'start of show feature'
with open(filename) as myFile:
for num, line in enumerate(myFile, 1):
if fromStr in line:
fromline = num
toString = 'end of show feature'
with open(filename) as myFile:
for num, line in enumerate(myFile, 1):
if toString in line:
toline = num
f=open(filepath)
lines=f.readlines()
print lines[toline]
note: filepath is a variable and it is the full path to the file & the filename itself.
print lines[toline]
works
print lines[fromline]
works
print lines[fromline..toline]
doesnt work, it errors out. I was wondering if i can get some help in this regard. I have referred this Reading specific lines only (Python) and Python read specific lines of text between two strings (this might help a but) but no go.
It would also greatly help if the extracted data/lines are stored into a variable.