0

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.

Community
  • 1
  • 1
Gajendra D Ambi
  • 3,832
  • 26
  • 30

2 Answers2

1

print lines[fromline..toline]

How about:

print lines[fromline:toline] 

It would also greatly help if the extracted data/lines are stored into a variable. doesnt work, it errors out.

How did you try that? Which error did you get?

DrJohn
  • 86
  • 1
  • 8
  • print lines[fromline:toline] kinda works but the output is like this 1 enabled \n', '\n', 'vrrp 1 disabled\n' , '\n', 'vrrpv3 1 disabled\n', '\n', 'vtp 1 disabled\n', '\n', '\n', ' end of show feature I guess I now need to figure out how to format the output properly. The lines are not one below the other but next to each other now. – Gajendra D Ambi Nov 04 '16 at 07:54
  • @GajendraDAmbi use `for` loop to pint line by line – furas Nov 04 '16 at 07:56
  • `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 store = lines[fromline:toline+1] cutstore = '\n'.join(store) print cutstore` Thank you @skycc @drjohn. The above code works now. – Gajendra D Ambi Nov 04 '16 at 07:58
1
filename = "somepath/somefilename.txt"
f=open(filename )
lines=f.readlines()

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

store =  lines[fromline:toline+1] 
cutstore = '\n'.join(store)
print cutstore

Thanks to skycc & drjohn, i was able to arrive at this solution.

Gajendra D Ambi
  • 3,832
  • 26
  • 30