Extract XML content from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract XML content from a file
# 8  
Old 01-18-2012
I don't understand your question. Do you want just the batch id in a variable?

Is this what you're looking for?
Code:
$ grep "<BATCH_ID>" input | sed 's:<[\/]*BATCH_ID>::g''
12345678519

# 9  
Old 01-18-2012
No.. The code which you given is working fine.
Code:
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
        print $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.
}' helium-core.log # Process the file 'inputfile'


Instead of passin the batch id "12345678519" , I have multiple ID which I came to know wen i run the script. So wants to pass a batch ID in a variable to the perl prog above . Like
Code:
elsif (!/\[<\?xml/ && !/<BATCH_ID>$BATCHID<\/BATCH_ID>/ && $f==1)

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-18-2012 at 06:43 AM.. Reason: Please use code tags for code and data samples, thank you
# 10  
Old 01-18-2012
perl code:
  1. #! /usr/bin/perl -w
  2. use strict;
  3.  
  4. my ($f, $x, $b_id);
  5. my @batch_id = ( # Put all batch_ids in this array
  6. 12345678519,
  7. 123456,
  8. );
  9.  
  10. open XML, "< input";
  11. for (<XML>) {
  12.     if (/\[<\?xml/../^\&#93;/) {
  13.         if (/\[<\?xml/) {
  14.             $f=0;
  15.             s/.*?(\[<\?xml.*)/$1/;
  16.             $x=$_;
  17.         }
  18.         elsif (/<BATCH_ID>[0-9]+?<\/BATCH_ID>/) {
  19.             for $b_id (@batch_id) {
  20.                 if (/<BATCH_ID>$b_id<\/BATCH_ID>/) { $f=1; print $x; print }
  21.             }
  22.         }
  23.         elsif (!/\&#91;<\?xml/ && !/<BATCH_ID>12345678519<\/BATCH_ID>/ && $f==1) { print }
  24.         elsif ($f==0) { next }
  25.     }
  26. }
# 11  
Old 01-18-2012
sorry for the confusion. I have to pass the batch id dyanamically.. Not multiple batch IDS. Only one batch ID where i will run it dynamically. Sorry
# 12  
Old 01-18-2012
Code:
#! /usr/bin/perl -w
use strict;

( @ARGV != 1 ) && die "Enter exactly one batch_id. Exiting";
# my @ARGV = ("12345678519"); # Un-comment this line and comment above line if you don't want to give batchid as parameter and want to define in script itself.
my ($f, $x);

open XML, "< input";
for (<XML>) {
    if (/\[<\?xml/../^\]/) {
        if (/\[<\?xml/) { $f=0; s/.*?(\[<\?xml.*)/$1/; $x=$_ }  
        elsif (/<BATCH_ID>$ARGV[0]<\/BATCH_ID>/) { $f=1; print $x; print }
        elsif (!/\[<\?xml/ && !/<BATCH_ID>12345678519<\/BATCH_ID>/ && $f==1) { print }
        elsif ($f==0) { next }
    }
}

Give batch id as parameter to script.
Code:
$ ./test.pl 12345678519
[<?xml version="1.0" encoding="UTF-8"?><ESP_SSIA_ACC_FEED>
<BATCH_ID>12345678519</BATCH_ID>
<UID>3498748823</UID>
<FEED_TYPE>FULL</FEED_TYPE>
<MART_NAME>SSIA_DM_TRANSACTIONS</MART_NAME>
<MART_TYPE>SSIA_TRANSACTIONS</MART_TYPE>
<CLIENT_ID>ESPDB</CLIENT_ID>
<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>
</ESP_SSIA_ACC_FEED>
]

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