From: Stu on 27 Apr 2010 16:22 All: Can somebody please point me in the write direction. I need to read an XML file and for every occurance of <name>NameNotFoundException</name> (ignore case) and than parse out the value of key under the detail tag. Here is an example of my XML file: <exception> <name>NameNotFoundException</name> - <details> <key>John D Smith(a)xyz.com</key> </details> </exception> <exception> <name>Found</name> - <details> <key>Jane Doe(a)xyz.com</key> </details> </exception> <exception> <name>NameNotFoundException</name> - <details> <key>Lisa A. Pepper(a)xyz.com</key> </details> </exception> ..... ..... ..... In the example above I should have the following values printed to a file: John D Smith(a)xyz.com Lisa A. Pepper(a)xyz.com Note: Jane Doe(a)xyz.com WOULD NOT print since <name>Found</name> and NOT <name>NameNotFoundException</name> Is there a powerful sed or awk command that can do this, which maybe I can run in a loop for each value of <exception>. Any examples would be greatly appreciated and thanks in advance for all that answer this post. DON'T WORRY THIS IS NOT MY HOMEWORK :-)
From: Seebs on 27 Apr 2010 17:55 On 2010-04-27, Stu <beefstu350(a)hotmail.com> wrote: > All: > > Can somebody please point me in the write direction. I need to read an > XML file and for > every occurance of <name>NameNotFoundException</name> (ignore case) > and than parse out the value of key under the detail tag. Use an XML parser. > Is there a powerful sed or awk command that can do this, which maybe I > can run in a loop for each value of <exception>. Any examples would be > greatly appreciated and thanks in advance for all that answer this > post. You could probably do it in awk, but it would be extremely wasteful and hard to get right. Keep in mind, the entire point of XML is that it's a standardized format so you don't have to keep writing parsers for it! -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Janis Papanagnou on 28 Apr 2010 04:33 Seebs schrieb: > On 2010-04-27, Stu <beefstu350(a)hotmail.com> wrote: >> All: >> >> Can somebody please point me in the write direction. I need to read an >> XML file and for >> every occurance of <name>NameNotFoundException</name> (ignore case) >> and than parse out the value of key under the detail tag. > > Use an XML parser. > >> Is there a powerful sed or awk command that can do this, which maybe I >> can run in a loop for each value of <exception>. Any examples would be >> greatly appreciated and thanks in advance for all that answer this >> post. > > You could probably do it in awk, but it would be extremely wasteful and > hard to get right. Keep in mind, the entire point of XML is that it's a > standardized format so you don't have to keep writing parsers for it! Or do both; use an XML enhanced awk (xgawk). @OP: Get xgawk if you want to do even more XML than the simple task that you describe. Janis > > -s
From: Janis Papanagnou on 28 Apr 2010 04:39 Stu schrieb: > All: > > Can somebody please point me in the write direction. I need to read an > XML file and for > every occurance of <name>NameNotFoundException</name> (ignore case) > and than parse out the value of key under the detail tag. > > Here is an example of my XML file: > > <exception> > <name>NameNotFoundException</name> > - <details> > <key>John D Smith(a)xyz.com</key> > </details> > </exception> > <exception> > <name>Found</name> > - <details> > <key>Jane Doe(a)xyz.com</key> > </details> > </exception> > <exception> > <name>NameNotFoundException</name> > - <details> > <key>Lisa A. Pepper(a)xyz.com</key> > </details> > </exception> > .... > .... > .... > > > In the example above I should have the following values printed to a > file: > > John D Smith(a)xyz.com > Lisa A. Pepper(a)xyz.com > > Note: Jane Doe(a)xyz.com WOULD NOT print since <name>Found</name> and > NOT <name>NameNotFoundException</name> > > Is there a powerful sed or awk command that can do this, which maybe I > can run in a loop for each value of <exception>. Any examples would be > greatly appreciated and thanks in advance for all that answer this > post. Assuming a regular structure in your XML file this will probably do... awk '/<name>/ { notfound = match ($0,/NameNotFoundException/) } /<key>/ && notfound { match ($0,/>.*</) print substr ($0,RSTART+1,RLENGTH-2) }' Janis > > DON'T WORRY THIS IS NOT MY HOMEWORK :-) >
From: Hermann Peifer on 28 Apr 2010 05:48 On 27/04/2010 22:22, Stu wrote: > All: > > Can somebody please point me in the write direction. I need to read an > XML file and for > every occurance of<name>NameNotFoundException</name> (ignore case) > and than parse out the value of key under the detail tag. > As others mentioned already: use XML tools for working with XML documents. One tool is xmlstarlet, see http://xmlstar.sourceforge.net/overview.php It looked a bit abandoned for the last years, but some developments happened in the last few weeks. An example usage would be: $ cat data.xml <root> <exception> <name>NameNotFoundException</name> <details> <key>John D Smith(a)xyz.com</key> </details> </exception> <exception> <name>Found</name> <details> <key>Jane Doe(a)xyz.com</key> </details> </exception> <exception> <name>NAMEnotFOUNDexception</name> <details> <key>Lisa A. Pepper(a)xyz.com</key> </details> </exception> </root> $ xmlstarlet sel -T -t -m /root/exception \ -i "translate(name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', \ 'abcdefghijklmnopqrstuvwxyz') = 'namenotfoundexception'" \ -v details/key -n data.xml John D Smith(a)xyz.com Lisa A. Pepper(a)xyz.com
|
Pages: 1 Prev: awk: replace previous newline on match Next: nyd frequency |