AWK: how to get average based on certain column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: how to get average based on certain column
# 1  
Old 03-24-2011
AWK: how to get average based on certain column

Hi,

I'm new to shell programming, can anyone help me on this? I want to do following operations -

1. Average salary for each country
2. Total salary for each city

and data that looks like -

salary country city
10000 zzz BN
25000 zzz BN
30000 zzz BN
10000 yyy ZN
15000 yyy ZN

My below code only gives average and sum of salaries in total for all records.

average) echo "Average salary for each country"
awk '{ s += $1; } END { print "sum: ", s, " average: ", s/NR, "no of records: ", NR }' $FILENAME
;;
totalsal) echo "Total salary for each city"
awk '{ s += $1; } END { print "sum of salaries : ", s }' $FILENAME
;;

output -

1. sum: 90000 average: 18000 no of records: 5
2. sum of salaries : 90000


i want this to be done based on column value i.e. -

for country as zzz -
sum: 65000 average: 21666.66 no of records: 3

for country as yyy -
sum: 25000 average: 12500 no of records: 2

Similarly for total salaries based on each City.

Is there any method in awk to make the search based on the value of certain column?
# 2  
Old 03-24-2011
Code:
awk 'NR>1{A[$2]+=$1;I[$2]++}END{for(i in A) if (A[i]) print "Country",i,"sum",A[i],"Average",A[i]/I[i],"records",I[i]}' infile

Code:
# cat tst
salary country city
10000 zzz BN
25000 zzz BN
30000 zzz BN
10000 yyy ZN
15000 yyy ZN

# awk 'NR>1{A[$2]+=$1;I[$2]++}END{for(i in A) if (A[i]) print "Country",i,"sum",A[i],"Average",A[i]/I[i],"records",I[i]}' tst
Country yyy sum 25000 Average 12500 records 2
Country zzz sum 65000 Average 21666.7 records 3
#

# 3  
Old 03-24-2011
Code:
awk '{a[$2]+=$1;b[$2]++}END{print "country\tsum\tavg\tnum";for (i in a){print i"\t"a[i]"\t"a[i]/b[i]"\t"b[i]}}' file

# 4  
Old 03-24-2011
Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check first column - average second column based on a condition

Hi, My input file Gene1 1 Gene1 2 Gene1 3 Gene1 0 Gene2 0 Gene2 0 Gene2 4 Gene2 8 Gene3 9 Gene3 9 Gene4 0 Condition: If the first column matches, then look in the second column. If there is a value of zero in the second column, then don't consider that record while averaging. ... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

2. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

3. UNIX for Dummies Questions & Answers

Average by specific column value, awk

Hi, I am searching for an awk-script that computes the mean values for the $2 column, but addicted to the values in the $1 column. It also should delete the unnecessary lines after computing... An example (for some reason I cant use the code tag button): cat list.txt 1 10 1 30 1 20... (2 Replies)
Discussion started by: bjoern456
2 Replies

4. Shell Programming and Scripting

Calculate the average of a column based on the value of another column

Hi, I would like to calculate the average of column 'y' based on the value of column 'pos'. For example, here is file1 id pos y c 11 1 220 aa 11 4333 207 f 11 5333 112 ee 11 11116 305 e 11 11117 310 r 11 22228 781 gg 11 ... (2 Replies)
Discussion started by: jackken007
2 Replies

5. UNIX for Dummies Questions & Answers

Find the average based on similar names in the first column

I have a table, say this: name1 num1 num2 num3 num4 name2 num5 num6 num7 num8 name3 num1 num3 num4 num9 name2 num8 num9 num1 num2 name2 num4 num5 num6 num4 name4 num4 num5 num7 num8 name5 num1 num3 num9 num7 name5 num6 num8 num3 num4 I want a code that will sort my data according... (4 Replies)
Discussion started by: FelipeAd
4 Replies

6. Shell Programming and Scripting

awk based script to find the average of all the columns in a data file

Hi All, I need the modification for the below mentioned code (found in one more post https://www.unix.com/shell-programming-scripting/27161-script-generate-average-values.html) to find the average values for all the columns(but for a specific rows) and print the averages side by side. I have... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

Average values in a column based on range

Hi i have data with two columns like below. I want to find average of column values like if the value in column 2 is between 0-250000 the average of column 1 is some xx and average of column2 is ww then if value is 250001-5000000 average of column 1 is yy and average of column 2 is zz. And my... (5 Replies)
Discussion started by: bhargavpbk88
5 Replies

8. Shell Programming and Scripting

Partial average of a column with awk

Hello, Let's assume I have 100 files FILE_${m} (0<m<101). Each of them contains 100 lines and 10 columns. I'd like to get in a file called "result" the average value of column 3, ONLY between lines 11 and 17, in order to plot that average as a function of the parameter m. So far I can compute... (6 Replies)
Discussion started by: DMini
6 Replies

9. UNIX for Dummies Questions & Answers

Average in awk based on time

Hi I am looking for an awk script which can compute the average of the last column based on the date and time. The file looks: site1,"2000-01-01 00:00:00", "2000-01-01 00:59:00",0.013 site2,"2000-02-01 01:00:00", "2000-02-01 01:59:00",0.035 site1,"2000-02-01 02:00:00", "2000-02-01... (15 Replies)
Discussion started by: kathy wang
15 Replies

10. UNIX for Dummies Questions & Answers

Use awk to calculate average of column 3

Suppose I have 500 files in a directory and I need to Use awk to calculate average of column 3 for each of the file, how would I do that? (6 Replies)
Discussion started by: grossgermany
6 Replies
Login or Register to Ask a Question