0

My query is simple to get the summation of values at column level in a text file using shell -- i.e. to add a new record in the text file which includes the sum.

For example here below:

name usersToday usersTommorrow    
Data1 92 181    
DataTwo 5 7    
Something 61 73    
Something_with_long_name 0 0

the desired output is

name usersToday usersTommorrow    
Data1 92 181   
DataTwo 5 7    
Something 61 73  
Something_with_long_name 0 0
Total 158 262

Please note that the text file will be updated with a new data column periodically everyday.

so on day 2 - post the commands for summation are updated the file will be like

name usersToday day2
Data1 92 181    
DataTwo 5 7    
Something 61 73    
Something_with_long_name 0 0
Total 158 262

On day 3 - post new data is appended the file will be like

name usersToday day2 day3
Data1 92 181 52
DataTwo 5 7 53
Something 61 73 25    
Something_with_long_name 0 0 26
Total 158 262 

so I want the summation for day3 needs to be updated.

1
  • 2
    Youre expected to include your code. We'll help you fix it, but we won't write it for you. Please read Help How-to-ask and Help On-Topic before asking more Qs here. Good luck. Commented May 30, 2019 at 12:06

1 Answer 1

3

Considering that your actual Input_file will be same as shown samples, could you please try following then.

awk 'FNR>1 && NF{first+=$2;second+=$3} 1; END{print "Total "first,second}' Input_file
Sign up to request clarification or add additional context in comments.

6 Comments

the file will be updated daily - with a new column being added everyday, so I will need to have summation of all including new column - probably summate everyday post data is appended.
@dynamicJos, need to take care of it, will check on it once.
@dynamicJos, do you need daily new total? or keep updating totals like total for today, total1 for yesterday etc etc in same file?
need to keep updating the total everyday (only for the new day and not do anything to the existing already summed up total of previous days)
@dynamicJos, not sure if I got it correctly, could you please show samples in your post once and let me know then?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.