Solved

parsing an xml file

Posted on 2013-06-27
3
301 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS EC2 HTTP & HTTPS 2 68
cscript to activate Windows and Office? 2 78
block mails from a particular country in postfix 7 47
Centos 7 DNS server not replying to clients 3 52
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 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