Extracting the value of an attribute tag from XML


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting the value of an attribute tag from XML
# 1  
Old 04-25-2010
Extracting the value of an attribute tag from XML

Greetings,

I am very new to the UNIX shell scripting and would like to learn. However, I am currently stuck on how to process the below sample of code from an XML file using UNIX comands:

Code:
<ATTRIBUTE NAME="Memory" VALUE="512MB"/>
<ATTRIBUTE NAME="CPU Speed" VALUE="3.0GHz"/>
<ATTRIBUTE NAME="Video Card" VALUE="4870"/>

Goal: Write a script to look for a certain attribute by name (i.e. "Memory") and return its value (i.e. "512MB" in this example).

Being new to shell scripting and UNIX in general, I had a very tough time trying to sort out whether to use awk, sed, grep, and the various other UNIX commands. My line of logic is to first grep the attribute name ("Memory") and I got that far, but I'm not sure how to cut the VALUE from the string.

Another frustrating thing is that the UNIX box I need to run this script on is pretty "vanilla" in the sense that it does not appear to have any sort of XML parsing tools such as xmlt, etc. so I am stuck with using the default UNIX commands.

If someone can kindly advise me on how to approach this issue, that would be much appreciated!
# 2  
Old 04-25-2010
try out the below command

Code:
kamaraj@kamaraj-laptop:~/Desktop/testing$ cat s.txt
<ATTRIBUTE NAME="Memory" VALUE="512MB"/>
<ATTRIBUTE NAME="CPU Speed" VALUE="3.0GHz"/>
<ATTRIBUTE NAME="Video Card" VALUE="4870"/>
kamaraj@kamaraj-laptop:~/Desktop/testing$ grep "Memory" s.txt | awk -F"\"" '{print $4}'
512MB
kamaraj@kamaraj-laptop:~/Desktop/testing$

or try this...

Code:
 awk -F"\"" ' /Memory/ {print $4}' a.xml

# 3  
Old 04-25-2010
Hello, JesterMania and welcome to the forums.

Assuming that all the relevant lines match the format of your sample data, if you have the value of the NAME attribute in a shell variable ($name in what follows), you can extract the value with the following AWK snippet:

Code:
$ name=Memory
$ awk -F\" -v n="$name" '/<ATTRIBUTE NAME="/ && $2==n {print $4}' data
512MB
$ name="CPU Speed"
$ awk -F\" -v n="$name" '/<ATTRIBUTE NAME="/ && $2==n {print $4}' data
3.0GHz
$ name="Video Card"
$ awk -F\" -v n="$name" '/<ATTRIBUTE NAME="/ && $2==n {print $4}' data
4870

Regards,
Alister
# 4  
Old 04-25-2010
Wow, thank you very much for the quick responses! Both solutions work equally well. As an aside, are there any books or online resources recommended to learn awk? It seems like a pretty powerful tool to me and I'd like to read up more on it.
# 5  
Old 04-25-2010
I only gave it a cursory glance, but Awk - A Tutorial and Introduction - by Bruce Barnett from the first page of google results for "awk" looks pretty good.
# 6  
Old 04-25-2010
Alister, looks like a good read - time to get practicing on my Linux box...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving XML tag/contents after specific XML tag within same file

Hi Forum. I have an XML file with the following requirement to move the <AdditionalAccountHolders> tag and its content right after the <accountHolderName> tag within the same file but I'm not sure how to accomplish this through a Unix script. Any feedback will be greatly appreciated. ... (19 Replies)
Discussion started by: pchang
19 Replies

2. Shell Programming and Scripting

Extracting the tag name from an xml file

Hi, My requirement is something like this, I have a xml file that contains some tags and nested tags, <n:tag_name1> <n:sub_tag1>val1</n:sub_tag1> <n:sub_tag2>val2</n:sub_tag2> </n:tag_name1> <n:tag_name2> <n:sub_tag1>value</n:sub_tag1> ... (6 Replies)
Discussion started by: Little
6 Replies

3. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

4. Shell Programming and Scripting

XML Parse between to tag with upper tag

Hi Guys Here is my Input : <?xml version="1.0" encoding="UTF-8"?> <xn:MeContext id="01736"> <xn:VsDataContainer id="01736"> <xn:attributes> <xn:vsDataType>vsDataMeContext</xn:vsDataType> ... (12 Replies)
Discussion started by: pareshkp
12 Replies

5. Shell Programming and Scripting

How to retrieve the value from XML tag whose end tag is in next line

Hi All, Find the following code: <Universal>D38x82j1JJ </Universal> I want to retrieve the value of <Universal> tag as below: Please help me. (3 Replies)
Discussion started by: mjavalkar
3 Replies

6. Shell Programming and Scripting

Extracting the value of an middle attribute tag from XML

Hi All, Please help me out in resolving this.. <secondTag enabled='true' processName='test1' pidFile='/tmp/test1.pid' /> From the above tag, I'm trying to retrieve the value of enabled and pidFile attributes by means of processName attribute. Would be thankful in resolving this..... (5 Replies)
Discussion started by: mjavalkar
5 Replies

7. Shell Programming and Scripting

read xml tag attribute and store it in variable

Hi, How to read xml tag attributes and store into variable in shell script? Thanks, Swetha (5 Replies)
Discussion started by: swetha123
5 Replies

8. Shell Programming and Scripting

command to remove attribute of an html tag

Is there any shell command to clean an html tag of its attributes. For ex <p align ="center"> with <p>. Thanks for your help!! (2 Replies)
Discussion started by: parshant_bvcoe
2 Replies

9. Shell Programming and Scripting

Extracting tag values from XML using perl

Hi All, I'm trying to extract the values for the 'src' and 'alt' tags within an xml file. In the files that I'm searching, the tags are always enclosed within an 'img' tag. Typically: <img src="diwiz01.gif" width="576" height="254" alt="Out-of-process and In-process COM Objects"><bookmark... (3 Replies)
Discussion started by: Steve_altius
3 Replies

10. Shell Programming and Scripting

Extracting XML Tag Contents

Hi Jean I require your help in writing a shell script. Iam zero in Unix programming. I have a large file about 400 MB of data, which contains about 50000 XML messages seperated by a Tab, I think. I need to extract only 4 values from each XML message and write it onto a new file. Please help me... (2 Replies)
Discussion started by: pk_eee
2 Replies
Login or Register to Ask a Question