![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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> |
|
||||
|
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 |
|
||||
|
Quote:
Perfect - thanks. Works as I needed. |
|
||||
|
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. ![]() |
|
||||
|
Can you provide more sample data and the expected output.
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|