Find the answer to your Linux question:
Results 1 to 2 of 2
For example, I have a text file with data which lists numerical values from two separate individuals Code: Person A 100 200 300 400 500 600 700 800 900 1000 ...
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    2

    [BASH] How to read multiple lines from a text file?

    For example, I have a text file with data which lists numerical values from two separate individuals

    Code:
    Person A
    100
    200
    300
    400
    500
    600
    700
    800
    900
    1000
    1100
    1200
    
    Person B
    1200
    1100
    1000
    900
    800
    700
    600
    500
    400
    300
    200
    100
    How would I go about reading the values for each Person, then being able to perform mathematical equations for each Person (finding the sum for example) ?

    Thanks.

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    Bash isn't really well suited to that, but awk will do it easily. You need just 2 lines in your awk script: one matching numeric lines which adds the numbers to your running total, the other matching text lines which prints the result and zeros the counter.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

Posting Permissions

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