Sum of Columns Base on First Column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum of Columns Base on First Column
# 1  
Old 03-30-2016
Sum of Columns Base on First Column

Input :-

Code:
Hd1;Hd2:hd3;Hd4;Hd5
X;1;2;3;4
Y;2;3;5;6
Z;3;5;6;7
X;10;11;24;16
Y;11;23;21;1
Z;10;13;14;15
X;0;1;2;0
K;0;0;0;0
K;0;0;0;0

I want Sum Data base on first column;

Code:
Hd1;Hd2:hd3;Hd4;Hd5
X;11;14;29;20
Y;12;26;26;7
Z;13;18;20;22
K;0;0;0;0

I Know how to do one Column but this input have multiples Column
Code:
awk  '{for(i=1;i<=NF;i++){if(i==1){Q=$1} else {A[Q OFS i]+=$i}}} END{for(y in A){W=y;gsub(/[[:space:]].*/,X,W);while(p<=NF){D[W]=D[W] OFS A[W OFS p];p++};while(k<=NF){delete A[W OFS k];k++};p="";k=""} { for(t in D){print t OFS D[t]}}}' Data.txt

I have Change space with ;
# 2  
Old 03-30-2016
The trick is to record which $1 values occur. Then you can use that to enumerate. Try:
Code:
awk '
  NR==1 {
    print
    next
  }

  {
    A[$1]
    for(j=2;j<=NF;j++) T[$1,j]+=$j
  }

  END {
    n=NF
    for(i in A) {
      $1=i
      for(j=2; j<=n; j++) $j=T[i,j]
      print
    }
  }
' FS=\; OFS=\; file

# 3  
Old 03-30-2016
can we wright in one Line!
# 4  
Old 03-30-2016
a bit verbose, but..
awk -f pare.awk FS=';' myFile.txt where pare.awk is:
Code:
FNR==1 {h=$0;next}
{
  for (i=2;i<=NF;i++)
    a[$1,i]+=$i
  n[$1]
  nf=NF
}
END {
  print h
  for(i in n) {
    printf i FS
    for(j=2;j<=nf;j++)
      printf("%d%s", a[i,j], (j==nf)?ORS:FS)
    }
}

---------- Post updated at 03:43 PM ---------- Previous update was at 03:41 PM ----------

Quote:
Originally Posted by pareshkp
can we wright in one Line!
why?
# 5  
Old 03-30-2016
No..That's Good....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy columns from one file into another and get sum of column values and row count

I have a file abc.csv, from which I need column 24(PurchaseOrder_TotalCost) to get the sum_of_amounts with date and row count into another file say output.csv abc.csv- UTF-8,,,,,,,,,,,,,,,,,,,,,,,,, ... (6 Replies)
Discussion started by: Tahir_M
6 Replies

2. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (12 Replies)
Discussion started by: as7951
12 Replies

3. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

4. Shell Programming and Scripting

Add Column base on other Column Data

HI Guys, I want add one extra Column base on 3rd Column . Input :- M204 MS204_154 :vsDataUeMe M204 MS204_154 es:sMeasure 0 M204 MS204_154 es:90ilterCoe 9 M204 MS204_154 es:searchE9090ortTime 40 M204 MS204_154 es:servOrPrioI90HoTimer 4000 M204 MS204_154 es:ueMeajllls154545 TRUE... (5 Replies)
Discussion started by: pareshkp
5 Replies

5. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

6. UNIX for Dummies Questions & Answers

Want the UNIX code - I want to sum of the 1st column wherever the first 2nd and 3rd columns r equal

I have the code for the below things.. File1 has the content as below 8859 0 subscriberCreate 18 0 subscriberPaymentMethodChange 1650 0 subscriberProfileUpdate 7668 0 subscriberStatusChange 13 4020100 subscriberProfileUpdate 1 4020129 subscriberStatusChange 2 4020307 subscriberCreate 8831... (5 Replies)
Discussion started by: Mahen
5 Replies

7. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

8. Shell Programming and Scripting

Change column to row base on column 2

Hi Guys, Input.txt L194 A -118.2 L194 B -115.1 L194 C -118.7 L196 A 0 L196 C 0 L197 A -111.2 L197 B -118.9 L197 C -119.9 L199 A -120.4 L199 B -119.9 ... (2 Replies)
Discussion started by: asavaliya
2 Replies

9. Shell Programming and Scripting

sum multiple columns based on column value

i have a file - it will be in sorted order on column 1 abc 0 1 abc 2 3 abc 3 5 def 1 7 def 0 1 -------- i'd like (awk maybe?) to get the results (any ideas)??? abc 5 9 def 1 8 (2 Replies)
Discussion started by: jjoe
2 Replies

10. Shell Programming and Scripting

help sum columns by break in first column with awk or sed or something.

I have some data that is something like this? item: onhand counted location ITEM0001 1 0 a1 ITEM0001 0 1 a2 ITEM0002 5 0 b5 ITEM0002 0 6 c1 I want to sum up... (6 Replies)
Discussion started by: syadnom
6 Replies
Login or Register to Ask a Question