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:

<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!

try out the below command

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...

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

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:

$ 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

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.

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.

Alister, looks like a good read - time to get practicing on my Linux box...