Sponsored Content
Full Discussion: Summing values in columns
Top Forums Shell Programming and Scripting Summing values in columns Post 302546712 by Dwee on Friday 12th of August 2011 01:35:15 AM
Old 08-12-2011
Summing values in columns

Basically I have to process a text file which has been sorted this way:

John 12
John 13
John 10
John 900
Peter 20
Peter 30
Peter 32

The first column is a name, and the second an arbitrary value, both delimited by a space. How can I sum them up such that it would become:

John 935
Peter 82

Preferably, this should be done in awk. I have actually written a simple function in bash to do that but it was horribly inefficient:

Code:
while read rec; do
        name=`echo $rec | cut -f1 -d" "`
	value=`echo $rec | cut -f2 -d" "`
	if [ "$name" = "$old_name" ]; then
		let "total=total+value"
        #for the first iteration, we have no old_name to print
	elif [ "$old_name" = "" ]; then
		total=$bytes
		old_IP=$next_IP
	else
		echo "$old_IP $total"
		total=$bytes
		old_IP=$next_IP
	fi
done < textfile

Thank you in advance.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Summing the columns of a file

Hi All, I have a file like - num.txt 12, 34, 65, line1 34, 65, 89, line2 43, 65, 77, line3 I want to do two things - 1. Add first three columns of each line and print the line with largest value. i.e. (12+34+65) for 1st line and so on. 2. Add middle column of each line i.e.... (3 Replies)
Discussion started by: asahlot
3 Replies

2. Shell Programming and Scripting

summing values of a column

I have a file which contains data as below: ----------------------------------------------------------------------------------------------- GSPWeb Statistics for the period of last 20 days... (3 Replies)
Discussion started by: mohsin.quazi
3 Replies

3. Shell Programming and Scripting

summing up iostat values in AIX

Friends, I have to run iostat -d on my AIX machine and print the sum of the output in tps column per iteration. can any one pls guide me how to do this using awk. here is the sample output iostat -d 2 2 | awk '!/System/ && !/Disks/ && !/cd/ && !/^$/ {print $4}' 2.0 3.0 1.0 3.0... (1 Reply)
Discussion started by: achak01
1 Replies

4. Shell Programming and Scripting

Awk: Summing values with group criteria

Hi Guys, I have a text file with ";" like separator F1;F2;F3;F4;F5 444;100041;IT;GLOB;1800000000 444;100041;TM;GLOB;1000000000 444;10300264;IT;GLOB;2000000000 444;10300264;IT;GLOB;2500000000 I have to sum the cullums F5 for same F2 and F3 collums The result must be: ... (7 Replies)
Discussion started by: gianluca2
7 Replies

5. Shell Programming and Scripting

reading a script and summing some values

I need help reading and summing some values in a file that looks like the following. This is an Oracle trace file. Oracle has as utility to do this,but it doesn't work properly unless my sql statement is done. I want to read the file and sum up some values to let me know how the query/job is... (0 Replies)
Discussion started by: guessingo
0 Replies

6. Homework & Coursework Questions

HELP with Unix scripts in summing columns in a file.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi guys, i'm a new guy here, and it's my first time creating a unix script. can you guys help me out here? i'd... (3 Replies)
Discussion started by: ramneim
3 Replies

7. Homework & Coursework Questions

HELP with Unix scripts in summing columns in a file

1. The problem statement, all variables and given/known data: Hi guys, i'm a new guy here, and it's my first time creating a unix script. can you guys help me out here? i'd really appreciate it. :( Here's my problem: This is the file i'm using, it has 6 columns, the first three columns are... (12 Replies)
Discussion started by: ramneim
12 Replies

8. Shell Programming and Scripting

Summing columns in line

I have a file with the following format AAAAA 1.34B 0.76B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.90B 0.00B 0.00B 0.46B 0.00B 0.03B 0.00B ... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

9. Shell Programming and Scripting

Summing columns over group of lines

I have an input file that looks like: ID1 V1 ID2 V2 P1 P2 P3 P4 ..... n no. of columns 1 1 1 1 1.0000 1.0000 1.0000 1.0000 1 1 1 2 0.9999 0.8888 0.7777 0.6666 1 2 1 1 0.8888 0.7777 0.6666 0.5555 1 2 1 2 0.7777 0.6666 0.5555 0.4444 2 1 1 1 0.6666 0.5555 0.4444 0.3333 2 1 1 2 0.5555 0.4444... (4 Replies)
Discussion started by: sdp
4 Replies

10. Shell Programming and Scripting

Summing up values of rows of numbers

data file contains failed=24 error=23 error=163 failed=36 error=903 i need to get a total count of each value above. i'm looking for the most efficient method to do this as the datafile i provided is just a sample. the actual data can be several hundred thousands of lines. so from... (3 Replies)
Discussion started by: SkySmart
3 Replies
Math::BigInt::FastCalc(3pm)				 Perl Programmers Reference Guide			       Math::BigInt::FastCalc(3pm)

NAME
Math::BigInt::FastCalc - Math::BigInt::Calc with some XS for more speed SYNOPSIS
Provides support for big integer calculations. Not intended to be used by other modules. Other modules which sport the same functions can also be used to support Math::BigInt, like Math::BigInt::GMP or Math::BigInt::Pari. DESCRIPTION
In order to allow for multiple big integer libraries, Math::BigInt was rewritten to use library modules for core math routines. Any module which follows the same API as this can be used instead by using the following: use Math::BigInt lib => 'libname'; 'libname' is either the long name ('Math::BigInt::Pari'), or only the short version like 'Pari'. To use this library: use Math::BigInt lib => 'FastCalc'; Note that from Math::BigInt v1.76 onwards, FastCalc will be loaded automatically, if possible. STORAGE
FastCalc works exactly like Calc, in stores the numbers in decimal form, chopped into parts. METHODS
The following functions are now implemented in FastCalc.xs: _is_odd _is_even _is_one _is_zero _is_two _is_ten _zero _one _two _ten _acmp _len _inc _dec __strip_zeros _copy LICENSE
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself. AUTHORS
Original math code by Mark Biggar, rewritten by Tels <http://bloodgate.com/> in late 2000. Separated from BigInt and shaped API with the help of John Peacock. Fixed, sped-up and enhanced by Tels http://bloodgate.com 2001-2003. Further streamlining (api_version 1 etc.) by Tels 2004-2007. Bug-fixing by Peter John Acklam <pjacklam@online.no> 2010-2011. SEE ALSO
Math::BigInt, Math::BigFloat, Math::BigInt::GMP, Math::BigInt::FastCalc and Math::BigInt::Pari. perl v5.16.2 2012-10-11 Math::BigInt::FastCalc(3pm)
All times are GMT -4. The time now is 09:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy