The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
A summing issue er_ashu Shell Programming and Scripting 5 05-06-2008 12:27 PM
how to read the data from an excel sheet and use those data as variable in the unix c Anne Grace UNIX for Advanced & Expert Users 1 03-03-2008 07:21 AM
summing according to the column ramky79 UNIX for Dummies Questions & Answers 2 02-28-2008 04:51 PM
Summing on column sbasetty Shell Programming and Scripting 2 02-07-2007 08:15 PM
egrep and grouping effigy Shell Programming and Scripting 1 10-14-2004 11:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-14-2007
pcharanraj pcharanraj is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 1
Grouping and summing data through unix

Hi everyone,



I need a help on Unix scripting.

I have a file is like this

Date Amt

20071205 10
20071204 10
20071203 200
20071204 300
20071203 400
20071205 140
20071203 100
20071205 100
20071205 10
20071205 300
20071203 10
20071204 100
20071204 100
20071205 700


I want to add all the Amt for a particular date and Return the Date having max amt.


So here is the consolidated total for individual dates.



20071203 710
20071204 510
20071205 1260



The script should return 20071205 for the above input since it has the max total amt


Can anyone provide the Unix shell script for the above requirement.

Thanks,
Charan.
  #2 (permalink)  
Old 12-14-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926

Code:
awk '{
	x[$1] += $2
	if (x[$1] > max) {
		max = x[$1]
		dt = $1
		} 
} END {
	print dt
}' data

For more than one entry with amt == max:


Code:
awk '{
	x[$1] += $2
	if (x[$1] > max) {
		max = x[$1]
		} 
} END {
	for (mdt in x)
		if (x[mdt] == max)
			print mdt
}' data


Last edited by radoulov; 12-14-2007 at 10:47 AM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0