Sponsored Content
Full Discussion: Grep XML tags
Top Forums Shell Programming and Scripting Grep XML tags Post 302090237 by ghostdog74 on Saturday 23rd of September 2006 11:46:19 PM
Old 09-24-2006
here's a Python alternative:

Code:
#!/usr/bin/python
import re
all = open("yourxml.xml").read()
pat = re.compile(r"<PRODID>.*</LANGUAGE>", re.MULTILINE|re.DOTALL)
for found in pat.findall(all):
	print found

Output:
Code:
<PRODID>LCTO84876</PRODID>
<PARTNUMBER>8872AC1</PARTNUMBER>
<WWPRODID>MODEL84876</WWPRODID>
<COUNTRY>US</COUNTRY>
<LANGUAGE>1</LANGUAGE>

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep xml tags

Hi I want to get the value between to XML tags as follows <EAN>12345</EAN> so i would want to return 12345. i have tried sed and awk but can't do it. can anyone help? (9 Replies)
Discussion started by: handak9
9 Replies

2. UNIX for Dummies Questions & Answers

Problems with grep and XML

I'm trying to use grep on XML files. The same grep expressions work on plaint text files but not on XML files (which of course are plain text files). Actually, these expressions work on XML files saved in DreamWeaver, but not when the same files are saved in XML Spy. I want grep to treat these... (4 Replies)
Discussion started by: pauljohn
4 Replies

3. Shell Programming and Scripting

Read content between xml tags with awk, grep, awk or what ever...

Hello, I trying to extract text that is surrounded by xml-tags. I tried this cat tst.xml | egrep "<SERVER>.*</SERVER>" |sed -e "s/<SERVER>\(.*\)<\/SERVER>/\1/"|tr "|" " " which works perfect, if the start-tag and the end-tag are in the same line, e.g.: <tag1>Hello Linux-Users</tag1> ... (5 Replies)
Discussion started by: Sebi0815
5 Replies

4. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

5. Shell Programming and Scripting

Shell Command to compare two xml lines while ignoring xml tags

I've got two different files and want to compare them. File 1 : HTML Code: <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record... (1 Reply)
Discussion started by: Shaishav Shah
1 Replies

6. Shell Programming and Scripting

Compare two xml files while ignoring some xml tags

I've got two different files and want to compare them. File 1 : <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record id="38,557"><columns><column><name>orge... (2 Replies)
Discussion started by: Shaishav Shah
2 Replies

7. Shell Programming and Scripting

How to add Xml tags to an existing xml using shell or awk?

Hi , I have a below xml: <ns:Body> <ns:result> <Date Month="June" Day="Monday:/> </ns:result> </ns:Body> i have a lookup abc.txtt text file with below details Month June July August Day Monday Tuesday Wednesday I need a output xml with below tags <ns:Body> <ns:result>... (2 Replies)
Discussion started by: Nevergivup
2 Replies

8. Shell Programming and Scripting

How to grep for a word in xml?

Hi, I have the below tag/s in my xml. <foreign-server name="MOHTASHIM_SERVER"> What will be the easist way to extract MOHTASHIM_SERVER without the double quotes "" from the above tag? Desired Output: (10 Replies)
Discussion started by: mohtashims
10 Replies

9. Shell Programming and Scripting

Getting string from xml tags using sed and grep

how to get string from tags using sed and grep i try to capture the tags : <groupId>org.test.proj.assent</groupId> <artifactId>mainapp</artifactId> <version>mainapp.1.4</version> <packaging>pom</packaging> <name>main app 1</name> and then from there i guess i will... (7 Replies)
Discussion started by: umen
7 Replies

10. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies
lfc_python(3)							 Python Reference						     lfc_python(3)

NAME
lfc - Python interface to the LFC lfcthr - Thread enabled version of Python interface to the LFC SYNOPSIS
import lfc import lfcthr lfcthr.init() DESCRIPTION
The lfc module permits you to access the LFC client interface from python programs. The lfc module is a swig wrapping of the standard C interface. For detailed descriptions of each function see the individual man page of each function. The lfcthr module is a version of the lfc module supporting multi-threaded Python clients. Its usage is similar to the usage of the lfc module except the obligatory initialisation call lfcthr.init() in the main program before threads are started. There follows a series of examples of how to use selected functions and how to retrieve the information returned by them: Examples are finding the GUID of an existing entry, listing the replicas of a given GUID and setting and retrieving the comment associated with an entry. EXAMPLE
#!/usr/bin/python import sys import lfc """ # stat an existing entry in the LFC and print the GUID """ name = "/grid/dteam/my.test" stat = lfc.lfc_filestatg() res = lfc.lfc_statg(name,"",stat) if res == 0: guid = stat.guid print "The GUID for " + name + " is " + guid else: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "There was an error while looking for " + name + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) EXAMPLE
#!/usr/bin/python import lfc """ # list the replicas of a given entry, starting from the GUID """ guid = "6a3164e0-a4d7-4abe-9f76-e3b8882735d1" listp = lfc.lfc_list() flag = lfc.CNS_LIST_BEGIN print "Listing replicas for GUID " + guid num_replicas=0 while(1): res = lfc.lfc_listreplica("",guid,flag,listp) flag = lfc.CNS_LIST_CONTINUE if res == None: break else: rep_name = res.sfn print "Replica: " + rep_name num_replicas = num_replicas + 1 lfc.lfc_listreplica("",guid,lfc.CNS_LIST_END,listp) print "Found " + str(num_replicas) + " replica(s)" EXAMPLE
#!/usr/bin/python import sys import lfc import re """ # setting and retrieving a comment on a file """ file = "/grid/dteam/my.test" comment = "MyComment" res = lfc.lfc_setcomment(file,comment) if res != 0: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "Problem while setting comment for " + file + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) buffer="" for i in range(0,lfc.CA_MAXCOMMENTLEN+1): buffer=buffer + " " res = lfc.lfc_getcomment(file,buffer) if res != 0: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "Problem while reading the comment for " + file + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) r = re.compile("(.*?) ", re.DOTALL) comment = r.findall(buffer)[0] print "Read back comment " + comment EXAMPLE
#!/usr/bin/python """ # Using the lfc_readdirxr method """ import sys import lfc name = "/grid/dteam/my.test" dir = lfc.lfc_opendirg(name,"") if (dir == None) or (dir == 0): err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "Error while looking for " + name + ": Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) while 1: read_pt = lfc.lfc_readdirxr(dir,"") if (read_pt == None) or (read_pt == 0): break entry, list = read_pt print entry.d_name try: for i in range(len(list)): print " ==> %s" % list[i].sfn except TypeError, x: print " ==> None" lfc.lfc_closedir(dir) EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getlinks method """ result, list = lfc.lfc_getlinks("/grid/dteam/antotests/extratests/dir2/f105", "") print result print len(list) if (result == 0): for i in list: print i.path EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getreplica method """ result, list = lfc.lfc_getreplica("/grid/dteam/antotests/extratests/dir2/f105", "", "") print result print len(list) if (result == 0): for i in list: print i.host print i.sfn EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getacl and lfc_setacl methods to add a user ACL """ nentries, acls_list = lfc.lfc_getacl("/grid/dteam/tests_sophie3", lfc.CA_MAXACLENTRIES) print nentries print len(acls_list) for i in acls_list: print i.a_type print i.a_id print i.a_perm # When adding a first ACL for a given user, you also need to add the mask # When adding the second user ACL, it is not necessary anymore acl_user = lfc.lfc_acl() acl_mask = lfc.lfc_acl() acl_user.a_type=2 # 2 corresponds to CNS_ACL_USER acl_user.a_id=18701 # user id acl_user.a_perm=5 acl_mask.a_type=5 # 5 corresponds to CNS_ACL_MASK acl_mask.a_id=0 # no user id specified acl_mask.a_perm=5 acls_list.append(acl_user) acls_list.append(acl_mask) res = lfc.lfc_setacl("/grid/dteam/tests_sophie3", acls_list) if res == 0: print "OK" else: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "There was an error : Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getacl and lfc_setacl methods to remove a user ACL """ nentries, acls_list = lfc.lfc_getacl("/grid/dteam/tests_sophie3", lfc.CA_MAXACLENTRIES) # Note : you cannot remove the owner ACL (i.e. for CNS_ACL_USER_OBJ type) if ACLs # ====== for other users exist. Ff all the other user ACLs are deleted, the owner # ====== ACL is automatically removed. for i in acls_list: print i.a_type print i.a_id print i.a_perm del acls_list[1] # delete a given user ACL from the list of ACLs res = lfc.lfc_setacl("/grid/dteam/tests_sophie3", acls_list) if res == 0: print "OK" else: err_num = lfc.cvar.serrno err_string = lfc.sstrerror(err_num) print "There was an error : Error " + str(err_num) + " (" + err_string + ")" sys.exit(1) EXAMPLE
#!/usr/bin/env python import lfcthr import os from threading import Thread class slave(Thread): def __init__ (self): Thread.__init__(self) def run(self): .... result = lfcthr.lfc_getreplica("", guid, "") .... return if __name__ == '__main__': os.environ['LFC_HOST'] = 'my_lfc.cern.ch' # Threaded library initialisation lfcthr.init() .... # Start up of threads for i in xrange(totalNumberOfSlaves): slv = slave(i) slv.start() .... EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getusrmap method """ result, list = lfc.lfc_getusrmap() print result print len(list) if (result == 0): for i in list: print i.userid + " " + i.username EXAMPLE
#!/usr/bin/python import lfc """ # Using the lfc_getgrpmap method """ result, list = lfc.lfc_getgrpmap() print result print len(list) if (result == 0): for i in list: print i.gid + " " + i.groupname KNOWN BUGS
The current interface to the lfc_getcomment(3), lfc_getcwd(3), lfc_readlink(3), lfc_seterrbuf(3) requires the passing of str object which is modified to contain the result (in a similar way to the C functions, which accept a buffer). However this breaks the immutability of python str. This will be changed in the future. SEE ALSO
LFC C interface man pages LFC
$Date: 2010-02-04 13:08:39 +0100 (Thu, 04 Feb 2010) $ lfc_python(3)
All times are GMT -4. The time now is 01:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy