Extract XML Element Values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract XML Element Values
# 1  
Old 10-24-2008
Extract XML Element Values

I have a rather large file with XML-style content. Each line contains one full XML entry. For example:

1:<Message><DNIS>1234</DNIS><UCID>3456</UCID><TransferGroup>XYZXYZ</TransferGroup></Message>
2:<Message><DNIS>9999</DNIS><UCID>2584</UCID><TransferGroup>ABCABC</TransferGroup></Message>
.....

I'm trying to figure a way to be able to go through the file, pull pertinent info, and print the output in a separate file delimited with commas:

output.txt:
1234,3456,XYZXYZ
9999,2584,ABCABC

I was able to achieve this via a series of greps with cuts for each value I want, but it takes far too long to run. I have 10,000+ entries to retrieve. I'm not very good with awk or sed or anything else that could prove more efficient. Can anyone help? Thanks!
# 2  
Old 10-24-2008
One sed should be enough, try:

Code:
sed '
    s#^<Message><DNIS>##
    s#</DNIS><UCID>#,#
    s#</UCID><TransferGroup>#,#
    s#</TransferGroup></Message>$##
' input.txt > output.txt


Last edited by Annihilannic; 10-24-2008 at 02:37 AM.. Reason: Changed slashes to hashes
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract values from xml file script

Hi, please help on this. I want extract values of xml file structure and print in determined way. <ProjectName> --> only appears once <StructList> --> is the top node <Struct> node --> could be more than 1 NameID, STX, STY, PRX, PRY --> appears only 1 time within each <Struct> node... (10 Replies)
Discussion started by: Ophiuchus
10 Replies

2. Shell Programming and Scripting

How to extract xml attribute values using awk inline.?

I am trying to extract specific XML attribute values for search pattern <factories.*baseQueueName' from resources.xml. my scripts works ok,, but to extract 3 values this code does echo $line three times, it could be 'n' times. How can I use awk to extract matching pattern values in-line or... (11 Replies)
Discussion started by: kchinnam
11 Replies

3. UNIX for Dummies Questions & Answers

Extract Element from XML file

<?xml version = '1.0' encoding =... (8 Replies)
Discussion started by: Siva SQL
8 Replies

4. Shell Programming and Scripting

Extract values from an XML File

Hi, I need to capture all the attributes with delete next to it. The source XML file is attached. The output should contain something like this below: Attributes = legacyExchangeDN Action = Delete Username = Hero Joker Loginid = joker09 OU =... (4 Replies)
Discussion started by: prvnrk
4 Replies

5. Shell Programming and Scripting

Find if XML element has a matching required element

I want to check if every <Part> element has corresponding <Description> in this sample XML. ....<Lot Of XML> <Inv lineNumber="2"> <Item> ... (4 Replies)
Discussion started by: kchinnam
4 Replies

6. Shell Programming and Scripting

Need to find root element name of XML file

Given this XML: <?xml version="1.0"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> ... (2 Replies)
Discussion started by: ricksj
2 Replies

7. Shell Programming and Scripting

Extracting the Root Element from the XML File

Any help to extract the root element from an XML file will be appreciated. Example: test.xml <?xml version="1.0" encoding="utf-8" ?> <TestXMLMessage> <TestRec> <ID>1000</ID> </TestRec> </TestXMLMessage> Wanted to extract the TestXMLMessage. Regards, Chari (6 Replies)
Discussion started by: sree_chari
6 Replies

8. UNIX Desktop Questions & Answers

read XML xml element with REGEXP

Hi, I would need to read an xml element from an xml file to a local variable. Please could you help me with a shell script to get so? Considering that I have a file called file.xml like below: <header> <description>This is the description</description> <content>This is the... (2 Replies)
Discussion started by: oscarmon
2 Replies

9. Shell Programming and Scripting

XML root element

Hi All Can someone please help me with this awk to search an element in a XML file with a particular value and then change the root element. Thanks & Regards Karan (9 Replies)
Discussion started by: karansachdeva
9 Replies

10. Shell Programming and Scripting

Finding a XML element and moving the file

Hi All, I am looking for a awk/shell which can find an element named REFERENCE in a XML file and check whether it is empty or not. If there is no value in the REFERENCE element then correspondingly move the file to some other folder. The Unix server is AIX version 4. Any inputs... (9 Replies)
Discussion started by: karansachdeva
9 Replies
Login or Register to Ask a Question