LinuxQuestions.org
Help answer threads with 0 replies.
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 03-31-2011, 01:12 AM   #1
ilukacevic
LQ Newbie
 
Registered: Jul 2010
Posts: 22

Rep: Reputation: 0
awk - summing over specific lines


Dear all,

I'm struggling myself with this problem: I have a file which contains a single column of data

2
.
.
.
45
65
.
.
.
23
87
.
.
.
43

I would like to have a results of the sum of the first n lines, then a results of the sum of the second n lines, then a results of the sum of the third n lines, and so on... all printed in a single column

2+...+45
65+...+23
87+...+43
.
.
.

Is there a way to do it with awk?

Thank you all in advance!

Yours,

Igor L.
 
Old 03-31-2011, 02:38 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,589

Rep: Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942
Quote:
Is there a way to do it with awk?
Yes

What have you tried and where are you getting stuck?
Remember the idea is we are here to help, not just do it for you.
 
Old 03-31-2011, 02:50 AM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Code:
$ ruby -ne 'BEGIN{s=0};s+=$_.to_i ; if $.%5==0 then puts s;s=0 ; end ' file
it says initialize s=0, accumulate s as we go along, check if the line number is mod 5 is 0 (every 5th line) , if it is , output s value and reset. Adapt it to which ever language/tool you are using.
 
1 members found this post helpful.
Old 03-31-2011, 02:55 AM   #4
ilukacevic
LQ Newbie
 
Registered: Jul 2010
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Yes

What have you tried and where are you getting stuck?
Remember the idea is we are here to help, not just do it for you.



I have tried with the following

awk '{ line[NR] = $0 }; sum = 0
END { for (t = 1; t <= 2; t++)
for (i = 1; i <= NR; i++) {
n = split(line[i], field)
printf(" %g", s=field[1]*field[3]/t+field[2]*field[4]/t)
sum = sum + s; printf(" %g", sum)
printf("\n")
}
}' proba.dat > proba1.dat



proba.dat contains the data. I have stuck with the issue that I just don't know how to make it stop summing when the first iteration over i ends, and start the new iteration with the sum=0. My try give me the sums of all the previous ones. And its in the 2nd column (but this is not such a big issue).

Igor
 
Old 03-31-2011, 03:05 AM   #5
ilukacevic
LQ Newbie
 
Registered: Jul 2010
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by kurumi View Post
Code:
$ ruby -ne 'BEGIN{s=0};s+=$_.to_i ; if $.%5==0 then puts s;s=0 ; end ' file
it says initialize s=0, accumulate s as we go along, check if the line number is mod 5 is 0 (every 5th line) , if it is , output s value and reset. Adapt it to which ever language/tool you are using.


Thanks!
What I get is

-e:1: undefined method `+' for nil:NilClass (NoMethodError)

I'll try to combine your solution with my try.


Igor
 
Old 03-31-2011, 03:19 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Following the suggestion by kurumi, you can check if the line number is a multiple of 5 and you can do it on-the-fly without storing all the file content in memory and parse it at the end. Example:
Code:
awk '{sum += $1}!(NR % 5){print sum; sum = 0}' file
Jusr remember the concept of true and false in awk.
 
1 members found this post helpful.
Old 03-31-2011, 03:37 AM   #7
ilukacevic
LQ Newbie
 
Registered: Jul 2010
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Following the suggestion by kurumi, you can check if the line number is a multiple of 5 and you can do it on-the-fly without storing all the file content in memory and parse it at the end. Example:
Code:
awk '{sum += $1}!(NR % 5){print sum; sum = 0}' file
Jusr remember the concept of true and false in awk.


Yes, I was just about playing with the if as kurumi proposed. So, taking both of yours advices, I have in the end (for the whole problem)

awk '{ line[NR] = $0 }
END { for (t = 1; t <= 2; t++)
for (i = 1; i <= NR; i++) {
n = split(line[i], field)
s=field[1]*field[3]/t+field[2]*field[4]/t
sum += s
printf(" %g", sum); sum=0
printf("\n")
}
}' proba.dat > proba1.dat

awk '{sum += $1}!(NR % 2){printf(" %g", sum); sum = 0; printf("\n")}' proba1.dat > proba2.dat


Which gives me what I wanted.

Thanks a lot!

Igor
 
Old 03-31-2011, 04:33 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,589

Rep: Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942Reputation: 1942
You do realise you can just put the newline in the first printf:
Code:
awk '{sum += $1}!(NR % 2){printf("%g\n", sum); sum = 0}' proba1.dat > proba2.dat
 
  


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
deleting lines from a file with specific pattern using AWK gandhigaurav1986 Programming 12 06-08-2010 02:08 AM
[SOLVED] Replace pattern in specific lines and column with AWK cgcamal Programming 10 04-26-2010 01:11 AM
Get all lines containing 23 specific words with AWK cgcamal Programming 3 11-05-2008 10:51 AM
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 08:59 AM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM


All times are GMT -5. The time now is 06:21 AM.

Main Menu
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