Sum up the column values group by using some field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum up the column values group by using some field
# 1  
Old 11-13-2012
Sum up the column values group by using some field

Code:
12-11-2012,PNL,158406
12-11-2012,RISK,4564
12-11-2012,VAR_1D,310101
12-11-2012,VAR_10D,310101
12-11-2012,CB,866
12-11-2012,STR_VAR_1D,298494
12-11-2012,STR_VAR_10D,309623
09-11-2012,PNL,1024106
09-11-2012,RISK,4565
09-11-2012,VAR_1D,317211
09-11-2012,VAR_10D,317211
09-11-2012,CB,985
09-11-2012,JOB,640
09-11-2012,STR_VAR_1D,305364
09-11-2012,STR_VAR_10D,317211

I have a flat file like above,
So all I need to sum up column 3 values if column 2 matches word VAR for coulmn 1 date

So foe 12 Nov I summed up VAR_1D, VAR_10D, STR_VAR_1D, STR_VAR_10D
same goes for 09 Nov
So my final file looks like


Code:
12-11-2012,PNL,158406
12-11-2012,RISK,4564
12-11-2012,TOTAL_VAR,1228319----->(which is sum of 310101(VAR_1D)+310101(VAR_10D)+298494(STR_VAR_1D)+309623(STR_VAR_10D) for date 12Nov)
12-11-2012,CB,866
09-11-2012,PNL,1024106
09-11-2012,RISK,4565
09-11-2012,TOTAL_VAR,1256997---->(317211(VAR_1D)+317211(VAR_10D)+305364(STR_VAR_1D)+317211(STR_VAR_10D) for date 09Nov)
09-11-2012,CB,985
09-11-2012,JOB,640

How to achieve this, summing up using group by as Date.

Last edited by manas_ranjan; 11-13-2012 at 07:52 AM.. Reason: changed i/p and o/p
# 2  
Old 11-13-2012
Code:
~$ perl -ne '@fields=split/,/;
if ($date==$fields[0]){
  if ($fields[1]=~/VAR/){
    $var_total+=$fields[2];
  }
  else {
    print 
  }
}
else{
  print "$date,TOTAL_VAR,$var_total\n" if $date;
  $date=$fields[0]
}
END{
  print "$date,TOTAL_VAR,$var_total\n";
}
' tmp/tmp.dat
12-11-2012,RISK,4564
12-11-2012,CB,866
12-11-2012,TOTAL_VAR,1228319
09-11-2012,RISK,4565
09-11-2012,JOB_AUDIT_DATA_TYPE,640
09-11-2012,TOTAL_VAR,2486301


Last edited by Skrynesaver; 11-13-2012 at 07:12 AM.. Reason: Added END block to catch final date in file
# 3  
Old 11-13-2012
thanks sky, but issue is I can't use perl, can I same suggestion in awk or something like that...
as well as O/p doesn't align with the requested o/p, total var sum for 2nd date 9 Nov is not summed up as well as some columns for 09th nov are missing

Last edited by manas_ranjan; 11-13-2012 at 07:59 AM..
# 4  
Old 11-13-2012
Code:
awk -F, 'NR==FNR{if($2 ~ /VAR/){X[$1]+=$3};next}{P=1;if($2 ~ /VAR/ ){if(! arr[$1]){$2="TOTAL_VAR";$3=X[$1];P=1;arr[$1]++}else{P=0}}}P' OFS="," file file

This User Gave Thanks to pamu For This Post:
# 5  
Old 11-13-2012
also try:
Code:
awk -F, '
$2 ~ /VAR/ {$2="TOTAL_VAR"};
{ a[$1","$2]+=$3; }
END { for (i in a) print i, a[i]; }
' OFS=, infile | sort

# 6  
Old 11-13-2012
Hi Pamu,

just in curious, what is the purpose of passing two times the file name at the end of command?

and if we want to format the output as below,
Code:
printf ("%10s %25s %10d\n",$1,$2,$3)

how to achieve ?
# 7  
Old 11-13-2012
try:
Code:
awk -F, '
$2 ~ /VAR/ {$2="TOTAL_VAR"};
{ a[$1","$2]+=$3; }
END {
  for (i in a) {s=i "," a[i];
    c=split(s,o,",");
    printf ("%10s %25s %10d\n",o[1],o[2],o[3]);
  }
}
' infile | sort

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sum the values in the column using date column

I have a file which need to be summed up using date column. I/P: 2017/01/01 a 10 2017/01/01 b 20 2017/01/01 c 40 2017/01/01 a 60 2017/01/01 b 50 2017/01/01 c 40 2017/01/01 a 20 2017/01/01 b 30 2017/01/01 c 40 2017/02/01 a 10 2017/02/01 b 20 2017/02/01 c 30 2017/02/01 a 10... (6 Replies)
Discussion started by: Booo
6 Replies

2. UNIX for Dummies Questions & Answers

Match sum of values in each column with the corresponding column value present in trailer record

Hi All, I have a requirement where I need to find sum of values from column D through O present in a CSV file and check whether the sum of each Individual column matches with the value present for that corresponding column present in the trailer record. For example, let's assume for column D... (9 Replies)
Discussion started by: tpk
9 Replies

3. Shell Programming and Scripting

Sum column values matching other field

this is part of a KT i am going thru. i am writing a script in bash shell, linux where i have 2 columns where 1st signifies the nth hour like 00, 01, 02...23 and 2nd the file size. sample data attached. Desired output is 3 columns which will give the nth hour, number of entries in nth hour and... (3 Replies)
Discussion started by: alpha_1
3 Replies

4. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

5. UNIX for Dummies Questions & Answers

awk to sum column field from duplicate row/lines

Hello, I am new to Linux environment , I working on Linux script which should send auto email based on the specific condition from log file. Below is the sample log file Name m/c usage abc xxx 10 abc xxx 20 abc xxx 5 xyz ... (6 Replies)
Discussion started by: asjaiswal
6 Replies

6. Shell Programming and Scripting

Getting a sum of column values

I have a file in the following layout: 201008005946873001846130058030701006131840000000000000000000 201008006784994001154259058033001009527844000000000000000000 201008007323067002418095058034801002418095000000000000000000 201008007697126001722141058029101002214158000000000000000000... (2 Replies)
Discussion started by: jclanc8
2 Replies

7. Shell Programming and Scripting

Sum of column by group wise

Hello All , I have a problem with summing of column by group Input File - COL_1,COL_2,COL_3,COL_4,COL_5,COL_6,COL_7,COL_8,COL_9,COL_10,COL_11 3010,21,1923D ,6,0,0.26,0,0.26,-0.26,1,200807 3010,21,192BI ,6,24558.97,1943.94,0,1943.94,22615.03,1,200807 3010,21,192BI... (8 Replies)
Discussion started by: jambesh
8 Replies

8. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

9. Shell Programming and Scripting

Column sum group by uniq records

Dear All, I want to get help for below case. I have a file like this. saman 1 gihan 2 saman 4 ravi 1 ravi 2 so i want to get the result, saman 5 gihan 2 ravi 3 like this. Pls help me. (17 Replies)
Discussion started by: Nayanajith
17 Replies

10. Shell Programming and Scripting

How to sum column 1 values

I have a file file like this. I want to sum all column 1 values. input A 2 A 3 A 4 B 4 B 2 Out put A 9 B 6 (3 Replies)
Discussion started by: suresh3566
3 Replies
Login or Register to Ask a Question