How To Sum Values Inside One Line In UNIX.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How To Sum Values Inside One Line In UNIX.?
# 1  
Old 11-26-2012
How To Sum Values Inside One Line In UNIX.?

Dears,
Good Day !

Plz; i want to sum-up two consecutive values inside one row, and put the summation in a new column.
here the input:
Code:
1 2
4 5
6 7

the output should be:
1 2 3
4 5 9
6 7 13

If someone can help me to solve my issue !
thx anyway.


Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 11-26-2012 at 04:06 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 11-26-2012
Code:
awk '{print $0,$1+$2}' infile

This User Gave Thanks to Jotne For This Post:
# 3  
Old 11-26-2012
thx Smilie
It works nicely
# 4  
Old 11-26-2012
If you have many number of column, or variable number of them, you can use
Code:
awk '{x=0; for(i=1; i<=NF; i++) x+=$i; print $0,x}'


Last edited by Jotne; 11-26-2012 at 05:23 AM..
This User Gave Thanks to Jotne For This Post:
# 5  
Old 11-26-2012
Thx again Jotne Smilie
very Useful And Clear !

but, may you help me to find maximum value in certain column number 3; and then to print all line content:

here are my Inputs:
1 2 3
4 5 9
6 7 13

Ant the Output should have only:
6 7 13

thx for your time
# 6  
Old 11-26-2012
Try

Code:
$ cat file
1 2
4 5
6 7
1 3
5 2
$ awk '{$(NF+1)=$1+$2;if($NF>a){a=$NF;p=$0}}END{print p}' file
6 7 13

Or

Code:
$ cat  file
1 2 3
4 5 9
6 7 13
1 3 4
5 2 7
$ awk '{if($NF>a){a=$NF;p=$0}}END{print p}' file
6 7 13


Last edited by pamu; 11-26-2012 at 06:07 AM..
This User Gave Thanks to pamu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to sum values on specific line number?

I have a file that looks like this: HP ColorPlotter Z-6100 ACMARTIN IP 192.168.x.x "VIRTUAL HP ( C9468A ) PART 1 of 2 (REAL CARTRIDGE 1)" "VIRTUAL HP ( C9468A ) PART 2 of 2 (REAL CARTRIDGE 1)" 181 181 "VIRTUAL HP ( C9471A ) PART 1 of 2 (REAL CARTRIDGE 2)" "VIRTUAL HP ( C9471A ) PART 2... (4 Replies)
Discussion started by: SysAdminRialto
4 Replies

2. Shell Programming and Scripting

How to sum the value with negative values?

Hi Gurus, I have requirement need to sum the value, the logic is if the value is negative then time -1, I tried below two ways. one is failed, another one doesn't work. awk -F"," '{if($8< 0 $8*-1 else $8) sum+=$8}{print sum, $8} END{printf("%.2f\n",sum)}' awk -F","... (4 Replies)
Discussion started by: ken6503
4 Replies

3. UNIX for Dummies Questions & Answers

Sum up values followed by pattern

I have a file with data merged from multiple files. File contains header, data and trailer of multiple files. The trailer starts with 99 and delimiter is ~. Trailer 99~120 99~30 As it is a merged file we i have multiple lines followed by 99~. Need help to find sum of values which are there... (4 Replies)
Discussion started by: santoshdrkr
4 Replies

4. UNIX for Dummies Questions & Answers

Unique values in a row sum the next column in UNIX

Hi would like to ask you guys any advise regarding my problem I have this kind of data file.txt 111111111,20 111111111,50 222222222,70 333333333,40 444444444,10 444444444,20 I need to get this file1.txt 111111111,70 222222222,70 333333333,40 444444444,30 using this code I can... (6 Replies)
Discussion started by: reks
6 Replies

5. Shell Programming and Scripting

How to fetch values from a line in a file to variables in UNIX?

Hi, I need to assign values from a lines in a file into variables in unix, i am using Korn shell. I tried the below script from posts but i am unable to fetch every value in a variable. #! /usr/bin/ksh #for file in test.txt; do IFS=$'\|' I=1 while read -a val do echo... (15 Replies)
Discussion started by: karthikram
15 Replies

6. UNIX for Dummies Questions & Answers

sum values based on ID

Hi, I would like to be able to sum up the counts of a column by the ID of another column. Example (although the actual file I have has thousands of IDs): Input file: A1BG-AS1:001 3 A1BG-AS1:002 0 A1BG-AS1:003 2 A1CF:001 1038 A1CF:002 105 A1CF:003 115 A1CF:004 137 Desired output... (3 Replies)
Discussion started by: fadista
3 Replies

7. Shell Programming and Scripting

Taking sum up all values inside the file

Hi, Taking sum up all values inside the file by using the below command: paste -sd+ filenmae | bc Getting some error like "0705-001: building space exceeded on line1 stdin" The original data looks like SPACE SPACE SPACE 0.123 JOBNAME1 SPACE SPACE 20.325 JOBNAME2 SPACE SPACE... (2 Replies)
Discussion started by: NareshN
2 Replies

8. Shell Programming and Scripting

How to sum up two decimal values?

I am running the following script : cat ind_sls_extr_UX.out_sorted | while read each_rec do count=`echo "${each_rec}" | cut -c1-2` if then final_amount=0 amount=`echo "${each_rec}" | cut -c280-287` echo "${amount}" final_amount=`expr ${amount} + ${amount}` ... (7 Replies)
Discussion started by: mady135
7 Replies

9. Shell Programming and Scripting

How to sum values from top

Hi. Im looking for way to sum numbers from top. For example i have such command top -b -n | grep Cpu | cut -c 35 - 39 which give me output 97.0 . Ho can i do with that value any arithmetic actions (for example 97.0 +1)? Using c = $((top -b -n | grep Cpu | cut -c 35 - 39)) gives me... (8 Replies)
Discussion started by: qdf
8 Replies

10. Shell Programming and Scripting

how to sum values from 2 different files?

Hi I am trying to add count values from two different files into one file. Could any body please suggest me best command to do this? My problem was as follows: a.txt b.txt c.txt 10 20 30(needed) i tried cat a.txt b.txt > c.txt (its not adding the values) Thanks in advance.. Praveen (8 Replies)
Discussion started by: npk2210
8 Replies
Login or Register to Ask a Question