LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-15-2011, 12:57 PM   #1
Alkass
Member
 
Registered: Mar 2010
Posts: 42

Rep: Reputation: 0
Sum numbers in a column in a specific block


Hello experts!

I have files which at some point have a block

</header>
<init>
2212 2212 0.35000000000E+04 0.35000000000E+04 0 0 10042 10042 3 4
0.36900423985E+05 0.32201153339E+02 0.11616800000E+01 3
0.16637486779E+05 0.14518702093E+02 0.11616800000E+01 2
0.41435863992E+04 0.36159004858E+01 0.11616800000E+01 1
0.40250283691E+03 0.35124408261E+00 0.11616800000E+01 0
</init>

So, I want to just add the lines in the first column in the <init> block , but NOT the

2212 2212 0.35000000000E+04 0.35000000000E+04 0 0 10042 10042 3 4

line, ie the first column from the

0.36900423985E+05 0.32201153339E+02 0.11616800000E+01 3
0.16637486779E+05 0.14518702093E+02 0.11616800000E+01 2
0.41435863992E+04 0.36159004858E+01 0.11616800000E+01 1
0.40250283691E+03 0.35124408261E+00 0.11616800000E+01 0

Normally, I would not have problem to do this, but I do not know how to constrain the script to just add the number inside the <init> block . Please note that my numbers are in scientific format, so I would need a similar output, ie not rounding


Thanks in advance
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 07-15-2011, 01:04 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I'd strongly consider writing this in Perl or Python.

Since you appear to already be familiar with bash, the learning curve will be minimal.

And since this appears to be an XML file, you'll have no trouble finding an XML library to help you parse the numbers from exactly the subnode(s) you're interested in.

'Hope that helps .. PSM

PS:
Here's one of many good links:
http://www.ibm.com/developerworks/library/xml-perl/
 
Old 07-15-2011, 01:22 PM   #3
Alkass
Member
 
Registered: Mar 2010
Posts: 42

Original Poster
Rep: Reputation: 0
Well, is not a xml file... is a special scientific format merged product of xml and text... So, let me put it this way, you are aware of a bash command to just constraint the add inside a specific block ?

Thanks again!
 
Old 07-15-2011, 02:12 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,686

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Well you would need to play with the formatting and it will also depend on whether init can appear more than once:
Code:
awk '/<\/?init>/{x = !x}x{a++}x && a > 2{sum+=$1}END{printf"%E", sum}' file
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sum the numbers between and including a and b palpankaj Programming 3 02-18-2010 09:52 AM
[PHP] Sum of a column by key kazuni Programming 4 11-09-2009 09:54 AM
calculating average of a column of numbers in bash hta1984 Programming 4 12-13-2006 12:58 PM
Show line and column numbers in Vim 7.0 judgex Linux - Desktop 1 08-29-2006 07:06 AM


All times are GMT -5. The time now is 01:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration