add coloum value based on first coloum


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add coloum value based on first coloum
# 1  
Old 05-19-2009
add coloum value based on first coloum

Hi i have a file and i want to add the coloum values
like
9067 is coming twice here so o/p willbe like
9067,sum of 3rd coloum,sum of 4th coloum
9015 is coming 0nce then o/p
9015,15,6

sum is happening based on first coloum it will add all the third coloum and 4th coloum value where first coloum matches

a.txt
9067,AE,11,2
9061,AA,12,3
9003,AB,13,4
9067,AA,14,5
9015,AA,15,6
9061,AD,16,7

o/p
9067,25,8
9061,28,10
9003,13,4
9015,15,6

i am using nawk -F "," '{ a[$1]++ ; sum[$1] += $3 } END { for (k in sum) print k "," sum[k] "," a[k]}' a.txt
but is is giving o/p
9003,13,1
9015,15,1
9061,28,2
9067,25,2

pls let me know the correct code
# 2  
Old 05-19-2009
You have done almost Smilie
Code:
 
 
awk -F "," '{ sum[$1] += $3;sum1[$1] +=$4 } END { for (k in sum) print k "," sum[k] "," sum1[k]}' a.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add user based on file

The script it should add all the users from this file "users.txt" All users should have the login shell as /sbin/nologin. When this script is called with any other argument, it should print the message as “Input File Not Found”ť. When this script is run without any argument, it should display... (1 Reply)
Discussion started by: invinzin21
1 Replies

2. UNIX for Beginners Questions & Answers

awk to add +1 to value based on condition in input

In the awk below I am trying to add a | that will adjust $2 in the ouput by adding +1 if the original value from file that was used in $3 had a - in it. Line 3 of file is an example of this. In my current awk I just subtract one but I am not sure how to only apply this to those values without a -.... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Add new column based in condition

At begining of column 2 the same block (2000) have 3 lines, in the next block (2336) it have 9 lines and for block (2524) 3 lines. ... (0 Replies)
Discussion started by: jiam912
0 Replies

4. UNIX for Beginners Questions & Answers

Add New Column Based on Files Name

Dear Sir, I need to add new column (last column) based on files name. my files name 20170809_target_tdc_pmx4.xls 20170809_target_tdc_pmx5.xls 20170809_target_tdc_pmx6.xls for example file : 20170809_target_tdc_pmx4.xls Item code Quantity Unit price Serial number... (1 Reply)
Discussion started by: radius
1 Replies

5. UNIX for Dummies Questions & Answers

Add 1 column based on name of files

Hi all, I already transferred list of files from ftp server with files name end with $Y$m$d Example my files data_20140101.xls data_20140102.xls data-20140103.xls and content of each files, for example data_20140101.xls USA|16846481|8871374|534909|0|0|0|700|5351981|0|31605445... (6 Replies)
Discussion started by: radius
6 Replies

6. Shell Programming and Scripting

How to add columns based on a pattern using awk?

Hi, I have a file with more than 1000 lines with ~14 columns. I need to find all the lines with matching value in column 14 and then add column 6 in all the lines before printing them out.. e.g if this is the input file: abc test input 10 for process 2345 abc test input 15 for process 2348... (1 Reply)
Discussion started by: xkdasari
1 Replies

7. Shell Programming and Scripting

add numbers to the output based on +/-

Add 35 to the 3rd col (12000) in input if it is "+" and it will be 12035 in output. Some thing like awk '{if ($3==+) print $2,$3,$3+35,$1 else print $2,$3-35,$3,$1}' + abc1 12000 - abc2 10000 + xyz1 11111 - vbcx 20036 + xy_z 33333 output abc1 ... (2 Replies)
Discussion started by: ruby_sgp
2 Replies

8. Shell Programming and Scripting

compare 2 coloum of 2 diff files using perl script

Hi, i am new to perl scripting.. i am still learing it.. i am asked to write a perl script which should compare 2 coloums of 2 different files. if those 2 coloumn are same the script should store the both the lines in 2 diff files. these are files, file 1: 21767016 226112 char 19136520... (3 Replies)
Discussion started by: vasuki
3 Replies

9. Shell Programming and Scripting

parse coloum in a file

I have a file like the format 000002371627240000023716272455577818030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001099001099425%00109925%IPY 2007013120070227Y99991231N YYX SRT0001010125%0000503318882HANDH800 R... (3 Replies)
Discussion started by: vaskarbasak
3 Replies

10. Shell Programming and Scripting

how to insert new coloum

Dear friends, I have a dataset like this 2.18 66.86 27.9 2.18 66.86 27.9 2.18 66.86 27.9 2.15 66.88 27.9 2.15 66.88 27.9 Now i have another dataset containing date coloum like this 22-12-2005 22-12-2005 22-12-2005 22-12-2005 22-12-2005 Now i want to... (3 Replies)
Discussion started by: rajan_ka1
3 Replies
Login or Register to Ask a Question