Pick the exact value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pick the exact value
# 8  
Old 01-22-2008
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.
Code:
# ./test.py
<C f="Val1" />
<C f="val2" />

# 9  
Old 01-22-2008
With Awk:

Code:
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.
# 10  
Old 01-22-2008
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.
# 11  
Old 01-22-2008
I tried this code:

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


Quote:
Traceback (most recent call last):
File "./test.py", line 5, in ?
if f.next().find("X_NAME1"):
AttributeError: 'file' object has no attribute 'next'
# 12  
Old 01-22-2008
Quote:
Originally Posted by ahmedwaseem2000
I tried this code:

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
Code:
#!/usr/bin/env python
data = open("file").readlines()
for i in range(len(data)):
    data[i] = data[i].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:
Code:
# ./test.py
['<C f="Val1" />', '<C f="val2" />']

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pick systems

looking for any advise on how to export Pick data from an AIX implementation. i have someone with limited experience that knows how to create a text file from a pick database. (1 Reply)
Discussion started by: jgt
1 Replies

2. UNIX and Linux Applications

Pick Operating System

Anyone know anything about "Advanced Plus Operating Environment". Preferably release 10 Revision 522Gcd probably dated 2003. (4 Replies)
Discussion started by: jgt
4 Replies

3. Shell Programming and Scripting

echo exact xml tag from an exact file

Im stumped on this one. Id like to echo into a .txt file all names for an xml feed in a huge folder. Can that be done?? Id need to echo <name>This name</name> in client.xml files. $path="/mnt/windows/path" echo 'recording names' cd "$path" for names in $path than Im stuck on... (2 Replies)
Discussion started by: graphicsman
2 Replies

4. Shell Programming and Scripting

Pick and print

Hi Friends, I'm facing an issue. I am having a file as under : a1 b1 c1 a2 a3 a4 b4 a5 b5 and I need to get the output as under: a1 b1 a4 b4 a5 b5 i.e. to pick the columns where a and b are consecutive and ignoring rest. I was trying some thing sily as: (2 Replies)
Discussion started by: vanand420
2 Replies

5. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

6. Shell Programming and Scripting

Pick one file from each subdirectory

Hi, I have a problem I am trying to solve with bash. I need to search in a file system (data base) with hundreds of directories and thousands of subdirectories and millions of files. The files have a specific format with a header that gives the properties. Directories are organized so... (1 Reply)
Discussion started by: majest
1 Replies

7. Shell Programming and Scripting

pick columns

I want to be able pick columns from 2 files with similar reference data: File 1 Last NameFirst NameSalaryCityDunnJohn $42,000.00 ChicagoGrantSuzy $95,000.00 GaryLoweMike $80,000.00 MilwaukeeGrantMike $59,000.00 JolietLoweKaren $48,000.00 South BendFile 2 TitleFirst NameLast... (2 Replies)
Discussion started by: sigh2010
2 Replies

8. Solaris

Please help me pick a machine

Hey guys I am new here and like this forum alot. I went to get into Solaris and would like to buy a sun box on ebay. NON rackmount. Problem is there is so many diff types. I am looking for one under the $800 range that can support solaris 10 and have the basics and decent ram. Can you guys... (11 Replies)
Discussion started by: Gnfanatic
11 Replies

9. Shell Programming and Scripting

how to pick out last charactor of a string?

Hi, Does anyone know how many ways to pick out last charactor of a string? Thanks! (7 Replies)
Discussion started by: cin2000
7 Replies
Login or Register to Ask a Question