Solved

parsing an xml file

Posted on 2013-06-27
3
300 Views
Last Modified: 2013-07-08
Hi all, I need a quick process to parse an XML file that has the below and I need to grab identify what the DEVICE number is attached to the SN. so per below I will need to look for the sn (405840461011130000) the value is below on line 6 and find its device ID on line 2, the value I'm looking for is the device the SN will be provided but the XML will have hundreds of them and I will need to find a specific SN and then get its device

<xmlroot>
<type value="DEVICE001">
<property id="PortType" valuetype="number" value="3"/>
<property id="DeviceID" valuetype="number" value="32"/>
<property id="ModelName" valuetype="string" value="TM-T88V"/>
<property id="PortName" valuetype="string" value="\\?\USB#VID_04b8&amp;PID_0202#405840461011130000"/>
<property id="DeviceType" valuetype="number" value="0"/>
<custom id="TM.PrinterID.2" valuetype="number" value="2"/>
0
Comment
Question by:atorex
3 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39282925
You could try something like this:
sn='405840461011130000'
awk  -F'[=|>]' -v sn="$sn" '
$0 ~ "<type value=" {d=$2}
$0 ~ sn {print sn" = "d;}
' test.xml

Open in new window

0
 

Author Comment

by:atorex
ID: 39284068
Thanks,
I will test this out ASAP.

regards,
0
 
LVL 27

Accepted Solution

by:
skullnobrains earned 500 total points
ID: 39289575
this is a bit ugly, but should be rather fast

cat xml | sed -n '1,/405840461011130000/ s/.*DEVICE\([0-9]*\).*/\1/p' | tail -n 1

the sed extracts and prints all the ids until it sees the required SN, the tail makes sure only the last one gets printed on screen
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Fetching data from Multiple Linux Server using Shell Scripting 55 154
installing docker on ubuntu 6 36
lunix and unix command 21 86
Check for Linux process in script 7 47
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Utilizing an array to gracefully append to a list of EmailAddresses
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question