Results 1 to 4 of 4
I'm kinda new at scripting, and I have the basics down, but here's a question I couldnt find an answer to:
I have a long text file full of numbers ...
- 07-06-2009 #1Just Joined!
- Join Date
- Jul 2009
- Posts
- 2
Need help - Bash script for file manipulation
I'm kinda new at scripting, and I have the basics down, but here's a question I couldnt find an answer to:
I have a long text file full of numbers (around 400). I have a script to sed the file so each line is a unique number. Now I want to add every 3 numbers.
So that if I have, lets say, 99 numbers in the file, my output will be 33 numbers (sum of num1+num2+num3, sum of num4+num5+num6, etc..).
Do I need to map the file into an array or is there some simple while-do loop for this?
Thank you!!
- 07-06-2009 #2Linux User
- Join Date
- Aug 2006
- Posts
- 458
s+=$0 means addition of each record BUT when NR%3==0 (means every third line. ) is hit, print out the sum and set back to 0Code:awk '{ s+=$0}NR%3==0{print s ;s=0}' file
- 07-06-2009 #3Just Joined!
- Join Date
- Jul 2009
- Posts
- 2
Thank you!!
Would you have time to explain what you did there?
If not, thank you again, much obliged
- 07-07-2009 #4Banned
- Join Date
- Jun 2009
- Posts
- 68
awk is very useful.


Reply With Quote