Extract XML message from a log file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract XML message from a log file using awk
# 1  
Old 06-21-2011
Extract XML message from a log file using awk

Dear all

I have a log file and the content like this

file name: temp.log
Code:
<?xml version="1.0" encoding="cp850"?>

<!DOCTYPE aaabbb SYSTEM '/dtdpath'>

<aaabbb>
  <tranDtl>
    <msgId>000001</msgId>
  </tranDtl>
.....

</aaabbb>
...
...
...
<?xml version="1.0" encoding="cp850"?>

<!DOCTYPE aaabbb SYSTEM '/dtdpath'>

<aaabbb>
  <tranDtl>
    <msgId>000002</msgId>
  </tranDtl>
.....

</aaabbb>

What I want to do is to extract the xml message and redirect it to a file called 000001.xml, 000002.xml etc...
start string = <?xml version="1.0" encoding="cp850"?>
end string = </aaabbb>

My idea is to get the start string (ss) and end string (es) line number first to a txt file, then use sed -n ss,eep > msgId.xml....
Code:
grep -n "xml version" temp.log | awk -F":" '{print $1}' | cat -n > start.txt
grep -n "</gcrLis>" temp.log | awk -F":" '{print $1}' | cat -n > end.txt
grep "<msgId>" temp.log | awk -F"[>,<]" '{print $3}' | cat -n > msgid.txt

Then loop the *.txt file and use sed to do so.

Although this approach is work but it is not pretty enough, how can I achieve this with awk. Please kindly advise.. Thank you so much.

Last edited by pludi; 06-22-2011 at 06:25 AM..
# 2  
Old 06-22-2011
With gnu coreutils you can:
Code:
% csplit -z testfile '/^<?xml/' '{*}'

You get your chunks in files xx00, xx01, etc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

Using SED/AWK to extract xml at end of file

Hello everyone, Firstly i do not require alot of help.. i am right at the end of finishing my scipt but cannot find a solution to the last part. What i need to do is, prompt the user for a file to work with, which i have done. promt the user for an output file - which is done. #!/bin/bash... (14 Replies)
Discussion started by: hugh86
14 Replies

4. Shell Programming and Scripting

How to extract part of xml line via awk?

Hi, I like to set a variable "name" automatically by reading an xml file. The name should be set to the date, which is a part of the following line of the xml file: <sceneID>C82_N32_A_SM_strip_008_R_2009-11-24T04:22:12.790028Z</sceneID> How can I separate this line, that the name will... (6 Replies)
Discussion started by: friend
6 Replies

5. Shell Programming and Scripting

how to extract part of xml line via awk?

Hi, I like to set a variable "name" automatically by reading an xml file. My code looks like this: set name = `awk '/<generationTime>/,/<\/generationTime>/ p' $xml_name` the "name" is thus set to <generationTime>2004-12-01T08:23:50.000000</generationTime> How can I separate this line,... (3 Replies)
Discussion started by: friend
3 Replies

6. Shell Programming and Scripting

Data Extract from XML Log File

Please help me out to extract the Data from the XML Log files. So here is the data ERROR|2010-08-26 00:05:52,958|SERIAL_ID=128279996|ST=2010-08-2600:05:52|DEVICE=113.2.21.12:601|TYPE=TransactionLog... (9 Replies)
Discussion started by: raghunsi
9 Replies

7. UNIX for Dummies Questions & Answers

Unable to extract a tag from a very long XML message

Hi I have a log file which contain XML message. I want to extract the value between the tag : <businessEventId>13201330</businessEventId> i.e., 13201330. I tried the following commands but as the message is very long, unable to do it. Attached is the log file. Please provide inputs. --... (3 Replies)
Discussion started by: Sapna_Sai
3 Replies

8. Shell Programming and Scripting

sed or awk to extract data from Xml file

Hi, I want to get data from Xml file by using sed or awk command. I want to get the following result : mon titre 1;Createur1;Dossier1 mon titre 1;Createur1;Dossier1 and save it in cvs file (fichier.cvs). FROM this Xml file (test.xml): <playlist version="1"> <trackList> <track>... (1 Reply)
Discussion started by: yeclota
1 Replies

9. Shell Programming and Scripting

Need to extract XML message from log

I need help to extract a following SOAP-ENV:Header XML message from the log. XML message need to be extracted: *************************** <SOAP-ENV:Header> <ServiceGatewayHeader> <SourceApplicationId>OXL</SourceApplicationId> <Version>1.0</Version> <UserId>TEST</UserId>... (4 Replies)
Discussion started by: tjshankar
4 Replies

10. Shell Programming and Scripting

extract data from xml- shell script using awk

Hi, This is the xml file that i have. - <front-servlet platform="WAS4.0" request-retriever="SiteMinder-aware" configuration-rescan-interval="60000"> <concurrency-throttle maximum-concurrency="50" redirect-page="/jsp/defaulterror.jsp" /> - <loggers> <instrumentation... (5 Replies)
Discussion started by: nishana
5 Replies
Login or Register to Ask a Question