0

First, I am VERY new to shell, Java, Python, Perl, and any other Linux based scripting language. I am in need of doing something that I would think seems relatively simple, but can't get it to work. I have used awk and xpath (like awk output better, but doesn't seem as easy to use).
First, I'll show you the .xml output I need to parse:

Blockquote

<CLIOutput> <Data> <Row> <Name>PROD3_A2_LUN10</Name> <Type>VMFS</Type> <Accessible>Yes</Accessible> <Hosts>esx01s112</Hosts> </Row> <Row> <Name>PRIVATE_VDI_SDLC3_A2_LUN174</Name> <Type>VMFS</Type> <Accessible>Yes</Accessible> <Hosts>esx02s104</Hosts> </Row> <Row> <Name>datastore1</Name> <Type>VMFS</Type> <Accessible>Yes</Accessible> <Hosts>esx01s100</Hosts> </Row> </data> </CLIOutput> 

I would ideally like to be able report not only on the XML attributes of "Name, Type, Accessible, and Hosts", but also report inside of those to only send an output that I want i.e. want to report on all "Name" that start with "P". My code attempts to date are:

* awk '$1~"^(" s ")$"{print $2}' RS=\< FS=\> s="Name" sdlcproxy1diff.xml -- shows me all "Name" items.| * xpath datastore.xml '/CLIOutput/Data/Row/*[self::Name]/text()' datastore.xml 2>/dev/null -- shows all "Name" items, but output is horrible. 

Thanks in advance!!

1
  • Expected output would just be either an .xml or .csv file that I could then run CLI commands against. I know I still need to work on the looping and whileread.do and all that, but I can't seem to even get past getting the stinking information exported lol.CommentedAug 13, 2012 at 19:33

1 Answer 1

1

You might want to look into a free application called 'xmlstarlet'. Google for it. It is very powerful, although the syntax can be a bit tough to get the hang of. Besides the many things it can do, it is able to pull fields out of the XML tree, and print them on a single line so you can sed/awk/grep them in a standard linux shellscript.

An example:

xmlstarlet sel -t -m /CLIOutput/data/Row -n -v "concat(Name,',',Type,',',Accessible,',',Hosts)" 

This would give you CSV converted output of your XML. At that stage you can use use a pipe and 'egrep' to find any lines where name starts with P:

grep -E '^P' 

Hope that helps.

3
  • This is the error I received when trying to run the command.XPath error : Invalid expression concat(Name,',',Type',',Accessible',',Hosts) ^ compilation error: element value-of xsl:value-of : could not compile select expression 'concat(Name,',',Type',',Accessible',',Hosts)'CommentedAug 14, 2012 at 14:13
  • I had to clean up the input from your post a bit. Change </data> to </Data>, then: cat intput.xml | xmlstarlet sel -t -m /CLIOutput/Data/Row -n -v "concat(Name,',',Type,',',Accessible,',',Hosts)" gives: PROD3_A2_LUN10,VMFS,Yes,esx01s112 etc.CommentedAug 16, 2012 at 13:30
  • (And apologies, there were two comma's missing from the example command I gave. I have made the edit to clean it up.)CommentedAug 16, 2012 at 13:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.