XML parsing with a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting XML parsing with a variable
# 1  
Old 03-06-2012
XML parsing with a variable

I have the following XML

Code:
 
<Audit_Type>1</Audit_Type><Session_Id>34505863</Session_Id>
<StatementId>1</StatementId><EntryId>1</EntryId>
<Extended_Timestamp>2012-03-06T10:25:20.789459</Extended_Timestamp>
<DB_User>KASINIY</DB_User>
<OS_User>majohn1</OS_User><OS_Process>28636</OS_Process>
<Terminal>CAIRVNBIS01</Terminal><Instance_Number>0</Instance_Number>
<Action>100</Action><TransactionId>0000000000000000</TransactionId><Returncode>0</Returncode><Comment_Text>Authenticated by: DATABASE; Client address: (ADDRESS=(PROTOCOL=tcp)(HOST=10.10.10.10)(PORT=4600))</Comment_Text><Priv_Used>5</Priv_Used>

Can somebody give me a SED/AWK statement that I can use to get
a spefic value.

To be efficient I would like the assign the value of a tag into a variable
and than use the variable in the parsing staement.

Ie

var=OS_User
sed $var .......

In addition, I may have 1 or many of these records in my file.

I may want to get the first, last or all occurances of $var

Thanks in advance to all who answer
# 2  
Old 03-06-2012
First occurrence:
Code:
perl -lns0e '/$tag>([^<]+)/;print $1' -- -tag=$var file

All occurrences:
Code:
perl -lns0e 'while (/$tag>([^<]+)/g){print $1}' -- -tag=$var file

Last occurrence:
Code:
perl -lns0e 'while (/$tag>([^<]+)/g){$x=$1};print $x' -- -tag=$var

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 03-06-2012
Bartus,

Thanks for your reply.. the code works great. Since I don't have perl
installed on all my machines, is there a similiar solution using sed or awk?
# 4  
Old 03-06-2012
Code:
$ awk -F'>' -v VAR="OS_User" -v RS='<' '/[/]/ { next }; ($1 == VAR) { print $2 }' data
majohn1

$

Code:
awk -F'>' -v VAR="OS_User" -v RS='<' '($1 == VAR) { print $2 }' data > /tmp/$$
while read USERNAME
do
        echo "got user $USERNAME"
done </tmp/$$
rm -f /tmp/$$

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

XML parsing

I have an xml file where the format looks like below <SESSIONCOMPONENT REFOBJECTNAME ="pre_session_command" REUSABLE ="NO" TYPE ="Pre-session command"> <TASK DESCRIPTION ="" NAME ="pre_session_command" REUSABLE ="NO" TYPE ="Command" VERSIONNUMBER ="1"> ... (8 Replies)
Discussion started by: r_t_1601
8 Replies

2. Shell Programming and Scripting

XML Parsing :

HI I want to parse below file in to two output :- Input :- ?xml version="1.0" encoding="UTF-8"?> <bulkCmConfigDataFile xmlns:un="utranNrm.xsd" <configData dnPrefix="Undefined"> <xn:SubNetwork id="ONRM_ROOT_MO_R"> <xn:MeContext id="C136"> ... (3 Replies)
Discussion started by: asavaliya
3 Replies

3. Shell Programming and Scripting

XML parsing

i have xml output in below format... <AlertsResponse> <Alert id="11216" name="fgdfg"> <AlertActionLog timestamp="1356521629778" user="admin" detail="Recovery Alert"/> </Alert> <Alert id="11215" name="gdfg <AlertActionLog timestamp="1356430119840" user=""... (12 Replies)
Discussion started by: vivek d r
12 Replies

4. Shell Programming and Scripting

XML: parsing of the Google contacts XML file

I am trying to parse the XML Google contact file using tools like xmllint and I even dived into the XSL Style Sheets using xsltproc but I get nowhere. I can not supply any sample file as it contains private data but you can download your own contacts using this script: #!/bin/sh # imports... (9 Replies)
Discussion started by: ripat
9 Replies

5. Shell Programming and Scripting

Parsing XML

I am trying to parse an xml file and trying to grab certain values and inserting them into database table. I have the following xml that I am parsing: <dd:service name="locator" link="false"> <dd:activation mode="manual" /> <dd:run mode="direct_persistent" proxified="false" managed="true"... (7 Replies)
Discussion started by: $criptKid617
7 Replies

6. UNIX for Advanced & Expert Users

XML Parsing

I had a big XML and from which I have to make a layout as below *TOTAL+CB | *CB+FX | CS |*IR | *TOTAL | -------------------------------------------------------------------------------------------------- |CB FX | | | | DMFXNY EMSGFX... (6 Replies)
Discussion started by: manas_ranjan
6 Replies

7. Shell Programming and Scripting

XML parsing

I have a xml file attached. I need to parse parameterId and its value My output should be like 151515 38 151522 32769 and so on.. Please help me. Its urgent (6 Replies)
Discussion started by: LavanyaP
6 Replies

8. Shell Programming and Scripting

parsing data from xml file is failing can't open variable

Created a korn shell script, everything is is working except this section, the variable $SYSINFO is being set, but the NASIP & NASDEV are failing, it appears to be treating the config.xml file config directory and xml as the file. Need a second set of eyes to tell me where I am messing up. #... (3 Replies)
Discussion started by: juanb25
3 Replies

9. Shell Programming and Scripting

XML Parsing

Hi, Need a script to parse the following xml file content <tag1 Name="val1"> <abc Name="key"/> <abc Name="pass">*********</abc> </tag1> <tag2 Name="Core"> <Host Name="a.b.c"> <tag1 Name="abc"> <abc Name="ac">None</abc> ... (4 Replies)
Discussion started by: Mavericc
4 Replies

10. Programming

XML parsing

Hi I want to take an XML file and transform it into a pipe-delimited format. What is the best tool to use for this? I have libxml2 which seems to be the best xml parser around. The xml file will have the following format. <Txn> <Date>120504</Date> <id>99</id> <Items> <Item>... (1 Reply)
Discussion started by: handak9
1 Replies
Login or Register to Ask a Question