The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl parsing compared to Ksh parsing popeye Shell Programming and Scripting 1 08-06-2008 11:46 PM
XML Log Parsing pk_eee Shell Programming and Scripting 3 07-25-2008 01:18 PM
parsing xml with awk/sed ricgamch Shell Programming and Scripting 3 05-28-2008 11:39 AM
Awk Parsing bombcan Shell Programming and Scripting 2 04-24-2008 03:45 PM
XML parsing handak9 High Level Programming 1 11-01-2004 08:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-15-2008
frustrated1 frustrated1 is offline
Registered User
  
 

Join Date: Aug 2003
Location: Ireland
Posts: 278
Parsing xml using awk - more help needed

As per another thread - How can I parse xml file?
I am using the following to extract the Subaccid and RecAccTotal from the xm file below

awk -v v=SubaccId -F'[<|>]' '$2==v{s=$3;getline;a[s]+=$3}END {for (i in a)print v,i,a[i]}' file


Can you tell me how I need to modify this if there are more fields in the xml response? (ie. for the RedAccType below)

ie. if there was additional information in a different response as below in red what do I need to change in the awk code?


XML FILE
<RecSubaccs>

<RecSubacc>
<SubaccId>1</SubaccId>
<RecAccTotal>0</RecAccTotal>
<RedAccType>Perm</RedAccType>
</RecSubacc>

<RecSubacc>
<SubaccId>2</SubaccId>
<RecAccTotal>0</RecAccTotal>
<RedAccType>Perm</RedAccType>
</RecSubacc>

<RecSubacc>
<SubaccId>3</SubaccId>
<RecAccTotal>0</RecAccTotal>
<RedAccType>Perm</RedAccType>
</RecSubacc>

</RecSubaccs>
  #2 (permalink)  
Old 09-15-2008
avronius avronius is offline VIP Member  
VIP Member
  
 

Join Date: Apr 2008
Location: Calgary
Posts: 305
If you do this in perl, you can create an array where each element in the array is a hash. (An array of hashes)
For the first RecSubacc, you'll have the following hash variables:
SubaccId 1
RecAccTotal 0
RedAccType Perm
As the file is read, if there are additional fields that appear between <RecSubacc> and </recSubacc>, it will simply create another hash.

You would use only the keys that you require - this would allow you to reuse those other keys at a later date, if your requirements change, without re-writing the entire script.

Just a thought....

Last edited by avronius; 09-15-2008 at 11:04 AM.. Reason: added bracketed text
  #3 (permalink)  
Old 09-15-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,350
I've tuned your code slightly:


Code:
awk -v v=SubaccId -F'[<|>]' '$2==v{s=$3;getline;a[s]+=$3;getline;t[s]=$3}END {for (i in a)print v,i,a[i],t[i]}' file

Regards
  #4 (permalink)  
Old 09-15-2008
frustrated1 frustrated1 is offline
Registered User
  
 

Join Date: Aug 2003
Location: Ireland
Posts: 278
Quote:
Originally Posted by Franklin52 View Post
I've tuned your code slightly:


Code:
awk -v v=SubaccId -F'[<|>]' '$2==v{s=$3;getline;a[s]+=$3;getline;t[s]=$3}END {for (i in a)print v,i,a[i],t[i]}' file

Regards

Perfect - thanks. Works as I needed.
  #5 (permalink)  
Old 09-15-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960
Working with XML files -
using all these tools like awk, shell scripting, sed everything would work

But IMHO
they are not maintainable,
quite difficult if you had to do some modification (just extend XPath or append something to the root element for example ),
will take more time for modification and testing.
In short, its not supported.

Instead there are wonderful perl modules available from CPAN.

Though, the initial time spent on learning and figuring out is more, its worth spending the time.

But for quick win cases, 1 time runs - all these awk/sed/shell scripting should be fine.

When the scripts that work on XML files need to be productionized or need to address a big set of file base, then they are not scalable way of doing them.

  #6 (permalink)  
Old 09-15-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,561
Quote:
Originally Posted by matrixmadhan View Post
Working with XML files -
using all these tools like awk, shell scripting, sed everything would work
xgawk
  #7 (permalink)  
Old 09-15-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,449
Quote:
Originally Posted by frustrated1 View Post
Can you tell me how I need to modify this if there are more fields in the xml response? (ie. for the RedAccType below)

ie. if there was additional information in a different response as below in red what do I need to change in the awk code?
Can you provide more sample data and the expected output.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0