Pick the exact value

Hi,

How do i pick a particular value from a group of lines using python.

for instance from the below sample. How do I pick Value=1 of OuterLines=1 and InnerLine=1

show what you have next time.

#!/usr/bin/python
f=open("file")
for line in f:
    if line.startswith("OuterLines=1"):
        if f.next().startswith("InnerLine=1"):
            print f.next()
f.close()        

But there could be multiple lines after the Innerline=1, In the below data i need all 6 value=1 lines

#!/usr/bin/python
f=open("file")
for line in f:
    if line.startswith("OuterLines=1"):
        if f.next().startswith("InnerLine=1"):
            while 1:
                N=f.next().strip()
                if N.startswith("InnerLine=2"): 
                    break
                print N
f.close()

But i wouldnt be knowing the Innerline=2 value. i'll have the Outerline=1 and innerline=1

then you make it break when the value is not "Value=1". otherwise, you have to provide a more concrete example of your sample data. I followed what is provided by you.

Below mentioned is the sample data. I would have the value of JS name( that is XXX of the first tag) and J name values ( that is X_NAME1 of the first tag). Using which i should find all the "C f" values

Thanks

using the second code I have posted, you would have gotten what you want. This is my output after a bit of fiddling with the patterns to search. I leave it to you to try yourself. Just substitute the values that you want to find.

# ./test.py
<C f="Val1" />
<C f="val2" />

With Awk:

awk '/^<C f=/ && jsf && jnf { print $2 }
$0 ~ "^<JS name=\""js { jsf = 1 }
$0 ~ "^<J name=\""jn { jnf = 1 }
/^<\/J>/ { jnf = 0 }
/^<\/JS/ { jsf = 0 }
' FS="[=/>]" js="XXX" jn="X_NAME1" filename

Use nawk or /usr/xpg4/bin/awk on Solaris.

Thank you all for the replies. I already have sed and awk solutions. I am looking for python solution. I have no clue about python, so I would appreciate if i could get an exact python script.

I tried this code:

#!/usr/bin/python
f=open("file")
for line in f:
    if line.find("XXX"):
        if f.next().find("X_NAME1"):
            while 1:
                N=f.next().strip()
                if N.startswith("</J>"): 
                    break
                print N
f.close()

but i am getting the error

what version of Python you are using? I think you have a very old version. If all else fails, try this

#!/usr/bin/env python
data = open("file").readlines()
for i in range(len(data)):
    data = data.strip()    
jsname_index = data.index("<JS name=\"XXX\">")
jend_index = data.index("</J>")
if data[jsname_index+1] == "<J name=\"X_NAME1\">" :
    print data[jsname_index+1+1 :jend_index ]

output:

# ./test.py
['<C f="Val1" />', '<C f="val2" />']