Grouping and summing data through unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grouping and summing data through unix
# 1  
Old 12-14-2007
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  
Old 12-14-2007
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Yearly Grouping of Data

I need some logic that would help to group up some records that fall between two dates: Input Data COL_1 COL_2 COL_3 COL_4 COL_5 COL_6 COL_7 COL_8 COL_9 COL_10 COL_11 COL_12 C ABC ABCD 3 ZZ WLOA 2015-12-01 2015-12-15 975.73 ZZZ P 147018.64 C ABC ... (3 Replies)
Discussion started by: Ads89
3 Replies

2. Shell Programming and Scripting

Mismatch in summing a column in UNIX

Hello, I am facing issue in summing up a column in unix.I am displaying a column sum up to 4 decimal places and below is the code snippet sed '1d' abc.csv | cut -d',' -f7 | awk '{s+=$1}END{ printf("%.4f\n",s)}' -170552450514.8603 example of data values in the column(not... (3 Replies)
Discussion started by: karthik adiga
3 Replies

3. Shell Programming and Scripting

UNIX grouping

Hi guys, I am a complete newbie to unix and have been tasked with creating a script to group the following data (file) by hourly slots so that I can count the transactions completed within the peak hour. I am not sure how to group data like this in unix. Can anyone please help? Here is an... (1 Reply)
Discussion started by: MrMidas
1 Replies

4. 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

5. 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

6. Shell Programming and Scripting

Summing data on N. row of each line

Hi friends, lets assume that I have lines of data as follows rico 12 bct 58 pot 65 vft 41 dek 45 kbt 13 her 35 ubr 14 . . . . . . . . . . . . I want to calculate the sum of the numbers at the 2. row of each... (6 Replies)
Discussion started by: rpf
6 Replies

7. Shell Programming and Scripting

Summing up rows data regarding 1st column

Dear all, I have one file like LABEL A B C D E F G H I J K L M N G02100 64651.3 25630.7 8225.21 51238 267324 268005 234001 52410.9 18598.2 10611 10754.7 122535 267170 36631.4 G02100 12030.3 8260.15 8569.91 ... (4 Replies)
Discussion started by: AAWT
4 Replies

8. Shell Programming and Scripting

Help with grouping data based on range position

Input file: data_1 1000 1290 data_4 290 234 data_2 1114 1110 data_5 534 999 data_6 900 1050 . . Desired_output_file_1_0_999: data_4 290 234 data_5 534 999 Desired_output_file_2_1000_1999: data_1 1000 1290 data_2 1114 1110 (1 Reply)
Discussion started by: perl_beginner
1 Replies

9. Shell Programming and Scripting

Grouping data numbers in a text file into prescribed intervals and count

I have a text file that contains numbers (listed from the smallest to the largest). For ex. 34 817 1145 1645 1759 1761 3368 3529 4311 4681 5187 5193 5199 5417 5682 . . (5 Replies)
Discussion started by: Lucky Ali
5 Replies

10. UNIX for Dummies Questions & Answers

Help with data grouping

Hi all, I have a set data as shown below, and i would like to eliminate the name that no children - boy and girl. What is the appropriate command can i use(other than grep)? Please assist... My input: name sex marital status children - boy children - girl ... (3 Replies)
Discussion started by: 793589
3 Replies
Login or Register to Ask a Question