Summing per group in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Summing per group in a loop
# 1  
Old 07-08-2015
Summing per group in a loop

I want to sum and average all other columns by first column

Code:
GR1     1       4       7
GR1     2       5       8
GR1     3       6       9
GR2     11      14      17
GR2     13      16      19
GR3     1       3       5
GR3     2       4       6

For a limited number of columns I can do

Code:
awk '{a[$1]+=$2;b[$1]+=$3;c[$1]+=$4} END {for (i in a) {print i,a[i],b[i],c[i]}}' file

to get the output

Code:
GR1 6 15 24
GR2 24 30 36
GR3 3 7 11

When the number of columns exceeds 4, a loop maybe required.
Here is where I need help from you guys.

How can I sum and average from col2 to colNF , with a loop?
# 2  
Old 07-08-2015
Here is an awk approach:-
Code:
awk '
        {
                T[$1]
                for ( i = 2; i <= NF; i++ )
                        A[$1 FS i] += $i
        }
        END {
                for ( k in T )
                {
                        printf "%s", k
                        for ( i = 2; i <= NF; i++ )
                                printf "\t%d", A[k FS i]
                        printf "\n"
                }
        }
' file

# 3  
Old 07-08-2015
Adaption of Yoda's suggestion to include averages:
Code:
awk '
        {
                T[$1]++
                for ( i = 2; i <= NF; i++ )
                        A[$1 FS i] += $i
        }
        END {
                for ( k in T )
                {
                        printf "%s", k
                        for ( i = 2; i <= NF; i++ )
                                printf "\t%2d,%6.2f", A[k FS i], A[k FS i]/T[k]
                        printf "\n"
                }
        }
' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Sql ORA-00937: not a single-group group function

I'm trying to return only one row with the highest value for PCT_MAX_USED. Any suggestions? When I add this code, I get the ORA-00937 error. trunc(max(decode( kbytes_max, 0, 0, (kbytes_alloc/kbytes_max)*100))) pct_max_used This is the original and returns all rows. select (select... (3 Replies)
Discussion started by: progkcp
3 Replies

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

3. Shell Programming and Scripting

Need to create an for loop for adding an disk in veritas volume group.

Hi Experts I need an script to add an disk in to the veritas volume manager disk group. For example: # cd /tmp # view disk c6t5d2 c6t2d1 c6t3d7 c6t11d2 c7t11d2 c6t11d6 Normally we add the disk like this: # vxdg -g freedg freedisk01=c6t5d2 # vxdg -g freedg freedisk02=c6t2d1 #... (3 Replies)
Discussion started by: indrajit_preet
3 Replies

4. Shell Programming and Scripting

need a one liner to grep a group info from /etc/group and use that result to search passwd file

/etc/group tiadm::345:mk789,po312,jo343,ju454,ko453,yx879,iy345,hn453 bin::2:root,daemon sys::3:root,bin,adm adm::4:root,daemon uucp::5:root /etc/passwd mk789:x:234:1::/export/home/dummy:/bin/sh po312:x:234:1::/export/home/dummy:/bin/sh ju454:x:234:1::/export/home/dummy:/bin/sh... (6 Replies)
Discussion started by: chidori
6 Replies

5. Shell Programming and Scripting

Sort the file contents in each group....print the group title as well

I've this file and need to sort the data in each group File would look like this ... cat file1.txt Reason : ABC 12345-0023 32123-5400 32442-5333 Reason : DEF 42523-3453 23345-3311 Reason : HIJ 454553-0001 I would like to sort each group on the last 4 fileds and print them... (11 Replies)
Discussion started by: prash184u
11 Replies

6. Shell Programming and Scripting

Merge group numbers and add a column containing group names

Hi All I do have a file like this with 6 columns. Groups of data merge together and the group number is indicated above each group. 1 1 12 26 289 3.2e-027 GCGTATGGCGGC 2 12 26 215 6.7e+006 TTCCACCTTTTG 3 9 26 175 ... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

7. Shell Programming and Scripting

Merge group numbers and add a column containing group names

I have a file in the following format. Groups of data merge together and the group number is indicated above each group. 1 adrf dfgr dfg 2 dfgr dfgr 3 dfef dfr fd 4 fgrt fgr fgg 5 fgrt fgr (3 Replies)
Discussion started by: Lucky Ali
3 Replies

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

9. Shell Programming and Scripting

How to group the output of a loop

Hi Guys, This is based on my question previously posted. :) I have my shell script like this: #!/usr/bin/sh e_id=`sqlplus -s scott/tiger@DB<<eof SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF; select emp_id from employee; quit ... (1 Reply)
Discussion started by: alvingo
1 Replies
Login or Register to Ask a Question