Sum of columns using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum of columns using awk
# 1  
Old 08-01-2017
Sum of columns using awk

Hello everyone

I am a beginner in Shell scripting. Need your help to achieve desired result.
I have a file (sample format below)

Code:
     001g8aX0007jxLz xxxxxxxxxxxxxxx           9213974926411    CO-COMM-133     CO-L001-DLY     7769995578239     44938           1    1       
     001g8aX0007jw8C xxxxxxxxxxxxxxx           9213974926411    CO-COMM-108     CO-L001-DLY     7769995578239     44199           1    1       
     001g8aX0001n69f xxxxxxxxxxxxxxx           9213974927136    CO-COMM-132     CO-L001-DLY     7769995583435     44273           2    2       
     001g8aX0000Pqsp xxxxxxxxxxxxxxx           9213974929242    CO-COMM-114     CO-L001-DLY     7769995598366     44901           2    2       
     001g8aX00023bjx xxxxxxxxxxxxxxx           9213974929991    ED-COMM-111     CO-L001-DLY     7769995602139     42943           2    2       
     001g8aX00023hCn xxxxxxxxxxxxxxx           9213974929991    CO-COMM-102     CO-L001-DLY     7769995602139     43012           2    2

Just imaging , i have 4th column '9213974926411' as an envelope and column 8 as page numbers in it. I need to find out total number of pages in one envelope and add the sum in each row as a last column. So the output will be like

Code:
     001g8aX0007jxLz xxxxxxxxxxxxxxx           9213974926411    CO-COMM-133     CO-L001-DLY     7769995578239     44938      1    1   2    
     001g8aX0007jw8C xxxxxxxxxxxxxxx           9213974926411    CO-COMM-108     CO-L001-DLY     7769995578239     44199      1    1   2   
     001g8aX0001n69f xxxxxxxxxxxxxxx           9213974927136    CO-COMM-132     CO-L001-DLY     7769995583435     44273      2    2   2    
     001g8aX0000Pqsp xxxxxxxxxxxxxxx           9213974929242    CO-COMM-114     CO-L001-DLY     7769995598366     44901      2    2   2    
     001g8aX00023bjx xxxxxxxxxxxxxxx           9213974929991    ED-COMM-111     CO-L001-DLY     7769995602139     42943      2    2   4   
     001g8aX00023hCn xxxxxxxxxxxxxxx           9213974929991    CO-COMM-102     CO-L001-DLY     7769995602139     43012      2    2   4

Thanks for help in advance !!

Last edited by Scrutinizer; 08-01-2017 at 05:47 AM.. Reason: code tags
# 2  
Old 08-01-2017
Try:
Code:
awk 'NR==FNR{A[$3]+=$8; next} {printf "%s%4d\n",$0,A[$3]}' file file


--
Note: the file name needs to be specified twice
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need Optimization shell/awk script to aggreagte (sum) for all the columns of Huge data file

Optimization shell/awk script to aggregate (sum) for all the columns of Huge data file File delimiter "|" Need to have Sum of all columns, with column number : aggregation (summation) for each column File not having the header Like below - Column 1 "Total Column 2 : "Total ... ...... (2 Replies)
Discussion started by: kartikirans
2 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. UNIX for Beginners Questions & Answers

Group by columns and add sum in new columns

Dear Experts, I have input file which is comma separated, has 4 columns like below, BRAND,COUNTRY,MODEL,COUNT NIKE,USA,DUMMY,5 NIKE,USA,ORIGINAL,10 PUMA,FRANCE,DUMMY,20 PUMA,FRANCE,ORIGINAL,15 ADIDAS,ITALY,DUMMY,50 ADIDAS,ITALY,ORIGINAL,50 SPIKE,CHINA,DUMMY,1O And expected output add... (2 Replies)
Discussion started by: ricky1991
2 Replies

5. Shell Programming and Scripting

awk sum of all columns needs to print exact amount

Hi I have attached txt file as input, and i'm able to calculate sum of columns at the end but the format of sum is not coming up right. awk -F"," '{for (i=4;i<=NF;i++) sum+=$i}{print}; END { sum="Total:"; for (i=1;i<=NF;i++) {printf sum ","} print "\n"}' input.txt check the o/p file, at... (6 Replies)
Discussion started by: manas_ranjan
6 Replies

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

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

awk sum columns

can anyone help me how do i add the colums using awk seperated by character @. for eg i have 3@4 2@9 5@1 the result should be 10 14 i tried using { sum+= $1 } END { print sum } but it just gives the result 10. can anyone help me with this one thank you and best regards (7 Replies)
Discussion started by: phone_book
7 Replies

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

10. Shell Programming and Scripting

Find percent between sum of 2 columns awk help

Hi I'm new to this forum and I'm a beginner when it comes to shell and awk programming. But I have the following problem: I have 5 csv files (data1.csv, data2.csv, etc.) and need to calculate the average between the total sum of the 1st and 7 column. csv example:... (3 Replies)
Discussion started by: sapo51
3 Replies
Login or Register to Ask a Question