Solved

parsing an xml file

Posted on 2013-06-27
3
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
Suggested Courses

617 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