Extract XML content from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract XML content from a file
# 1  
Old 01-18-2012
Extract XML content from a file

Code:
310439 2012-01-11 03:44:42,291 [tomcat-exec-11]  INFO PutServlet:? - Content of the Message is:[<?xml version="1.0" encoding="UTF-8"?><ESP_SSIA_ACC_FEED>
 310440 <BATCH_ID>12345678519</BATCH_ID>
 310441 <UID>3498748823</UID>
 310442 <FEED_TYPE>FULL</FEED_TYPE>
 310443 <MART_NAME>SSIA_DM_TRANSACTIONS</MART_NAME>
 310444 <MART_TYPE>SSIA_TRANSACTIONS</MART_TYPE>
 310445 <CLIENT_ID>ESPDB</CLIENT_ID>
 310446 <SQL>FROM  SSIA_DM_TRANSACTIONS  WHERE   ASAT BETWEEN '2012-01-10T03:44:48.385' and '2012-01-11T03:43:46.646' AND       SSIA_ACCOUNT_CODE = 'BTAC2091' AND PORTF        OLIO_CODE = '02091' </SQL>
 310447 </ESP_SSIA_ACC_FEED>
 310448 ]

The above is the XML code .. It starts with "[<?xml version" and end with "]" .. I want to extract this from a file which contain other datas also. Any one can help in this. Note "[<?xml version" starts from any parth the line .. 310439 .. 310448 are line numbers here

Last edited by Franklin52; 01-18-2012 at 04:50 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-18-2012
I've removed line numbers from 'inputfile' before passing it to below perl one-liner.
Code:
perl -ne 'if(/\[<\?xml/../^\]/){(/\[<\?xml/)&&s/.*?(\[<\?xml.*)/$1/;print}' inputfile

# 3  
Old 01-18-2012
Wow .. great .

Further , I want the xml code which has the ID "12345678519".. Batch ID here ( Second line) .
Please help me out
# 4  
Old 01-18-2012
Code:
perl -ne '
if (/\[<\?xml/../^\]/) {
    if (/\[<\?xml/) {
        $f=0;
        s/.*?(\[<\?xml.*)/$1/;
        $x=$_;
    }
    elsif (/<BATCH_ID>12345678519<\/BATCH_ID>/) {
        $f=1;
        print $x; print;
    }
    elsif (!/\[<\?xml/ && !/<BATCH_ID>12345678519<\/BATCH_ID>/ && $f==1) { print }
    elsif ($f==0) { next }
}' inputfile

# 5  
Old 01-18-2012
Also It would be great if you explain the perl code which you given. It is hard to understand for me . Thanks in Advance
# 6  
Old 01-18-2012
Code:
perl -ne ' # Process each line of file
if (/\[<\?xml/../^\]/) { # Read all those lines from file between (and including) lines containing "<?xml" to "]"
    if (/\[<\?xml/) { # If line contains "<?xml"
        $f=0; # set a flag to zero
        s/.*?(\[<\?xml.*)/$1/; # Remove text before "<?xml" in line containing "<?xml", or rather keep text from "<?xml" till end of line
        $x=$_; # Store the edited value in $x
    }
    elsif (/<BATCH_ID>12345678519<\/BATCH_ID>/) { # Check if line contains <BATCH_ID>12345678519</BATCH_ID>
        $f=1; # Set flag to one
        print $x; print; # Print line containing "<?xml" and line containing batch id
    }
    elsif (!/\[<\?xml/ && !/<BATCH_ID>12345678519<\/BATCH_ID>/ && $f==1) { print } # If line doesnt contain "<?xml" and batch id and if flag is set to one, then print the line
    elsif ($f==0) { next } # If flag is zero while processing a line, skip it.
}' inputfile # Process the file 'inputfile'

# 7  
Old 01-18-2012
I have to pass the batch in a variable. I tried but finally am not able to do it .. ;-(

---------- Post updated at 05:26 AM ---------- Previous update was at 05:20 AM ----------

Thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep content in xml file

I have an xml file with header as below. <Provider xmlns="http://www.xyzx.gov/xyz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xyzx.gov/xyz xyz.xsd" SCHEMA_VERSION="2.5" PROVIDER="5"> I want to get the schema version here that is 2.5 and put in a... (7 Replies)
Discussion started by: Ariean
7 Replies

2. Shell Programming and Scripting

Extracting content from xml file

Hello All, Hope you are doing well!!!!! I have a small code in the below format in xml file: <UML:ModelElement.taggedValue> <UML:TaggedValue tag="documentation" value="This sequence&#xA;&#xA;HLD_EA_0001X&#xA;HLD_DOORS_002X"/> <UML:TaggedValue tag="documentation" value="This... (11 Replies)
Discussion started by: suvendu4urs
11 Replies

3. Shell Programming and Scripting

Extract a particular xml only from an xml jar file

Hi..need help on how to extract a particular xml file only from an xml jar file... thanks! (2 Replies)
Discussion started by: qwerty000
2 Replies

4. Shell Programming and Scripting

Create xml file using a content from another xml file

I need to create a xml file(master.xml) with contents from another xml files(children). I have below list of xml files in a temporary location (C:/temp/xmls) 1. child1.xml 2. child2.xml Below is the content of the child1.xml & child2.xml files, child1.xml <root> <emp> ... (3 Replies)
Discussion started by: vel4ever
3 Replies

5. Shell Programming and Scripting

Extract Content from a file

I have an input file with contents like: ./prbru6/12030613.LOG:24514|APPL|prbru6.8269.RTUDaemon.1|?|13:49:56|12/03/06|GMT+3|?|RTUServer Error:Count of Internal Error Qty (-1) < 0, for Audit group id - 1L5XVJ6DQE36AXL, after record number,1, File: EventAuditor.cc, Line: 394|? ... (5 Replies)
Discussion started by: rkrish
5 Replies

6. Shell Programming and Scripting

perl extract content of file

I'm using Mail::Internet module, which will basically filter through email content and extract the body of the message my perl script to extract the body of the email #!/usr/bin/perl -w use Mail::Internet; @lines = <STDIN>; $mi_obj = new Mail::Internet(); ... (2 Replies)
Discussion started by: amlife
2 Replies

7. Shell Programming and Scripting

Need to replace particular content in a xml file

Hi, My requirement is to find a text and replace it with another in a XML file. I am new to Unix,Please provide some suggestion to achieve. Find: <Style ss:ID="ColumnHeader1"> Replace with: <Style ss:ID="ColumnHeader1"> <Borders> <Border ss:Position="Bottom"... (4 Replies)
Discussion started by: cnraja
4 Replies

8. Shell Programming and Scripting

Extract specific content from a file

My input file: >sequence_1 ASSSSSSSSSSSDDDDDDDDDDDCCCCCCC ASDSFDFFDFDFFWERERERERFSDFESFSFD >sequence_2 ASDFDFDFFDDFFDFDSFDSFDFSDFSDFDSFASDSADSADASD ASDFFDFDFASFASFASFAFSFFSDASFASFASFAFS >sequence_3 VEDFGSDGSDGSDGSDGSDGSDGSDG dDFSDFSDFSDFSDFSDFSDFSDFSDF SDGFDGSFDGSGSDGSDGSDGSDGSDG My... (22 Replies)
Discussion started by: patrick87
22 Replies

9. Shell Programming and Scripting

appending content in a xml file

Please help me on this..... i have a file which has following content: <IPCoreProducerConfig> <Producer> <config> <key>machineId</key> <value>machine1</value> </config> <config> ... (4 Replies)
Discussion started by: Aditya.Gurgaon
4 Replies

10. Shell Programming and Scripting

Content extract of a file using awk

Hi Everyone, I have a file with the below content: File1.txt ====== ### ###==> the below table was created for testing1 purpose; ### create table 123 ( field1 date, field2 char(10) primary key(field1) ); ### ###==> the below table was created... (5 Replies)
Discussion started by: nr_shan
5 Replies
Login or Register to Ask a Question