Find the answer to your Linux question:
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 ...
  1. #1
    Just 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!!

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    awk '{ s+=$0}NR%3==0{print s ;s=0}' file
    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 0

  3. #3
    Just 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

  4. #4
    Banned
    Join Date
    Jun 2009
    Posts
    68
    awk is very useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...