Adding matching field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding matching field
# 1  
Old 01-06-2012
Adding matching field

I have a flat file
test.log
Code:
red,5,,,,,
green,7,,,,,
blue,4,,,,,
red,8,,,,,
green,9,,,,,

How i get a a result:
Code:
blue,4,,,,,
green,16,,,,,
red,13,,,,,

Thanks

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 01-06-2012 at 06:53 PM..
# 2  
Old 01-06-2012
Something like this:

Code:
awk -F, 'END {
  for (_0_ in _0)
    print _0[_0_]
  }
{
  $2 = _2[$1] += $2
  _0[$1] = $0
  }' OFS=, infile

This User Gave Thanks to radoulov For This Post:
# 3  
Old 01-06-2012
Code:
nawk -F, '{a[$1]+=$2;fld=NF}END{for(i in a) {printf("%s%c%s",i,OFS,a[i]);$(fld-2)=OFS;print}}' OFS=, myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Adding field to file

Hi I have file as given below 040|14300|40.0|563000 042|13200000|40.0 041|100|40.0 043|10000|40.0 045|102|40.0 I want to check if field 4 is null then I have to place | in position 4. result set should be 040|14300|40.0|563000 042|13200000|40.0| 041|100|40.0| 043|10000|40.0|... (9 Replies)
Discussion started by: shabeena
9 Replies

3. Shell Programming and Scripting

Awk: adding fields after matching $1

Dear AWK-experts! I did get stuck in the task of combining files after matching fields, so I'm still awkward with learning AWK. There are 2 files: one containing 3 columns with ID, coding status, and score for long noncoding RNAs: file1 (1.txt) (>5000 lines) ... (12 Replies)
Discussion started by: kben
12 Replies

4. UNIX for Dummies Questions & Answers

Sed: Adding new line after matching pattern

Hi I just wanted to add a new line after every matching pattern: The method doing this doesn't matter, however, I have been using sed and this is what I tried doing, knowing that I am a bit off: sed 'Wf a\'/n'/g' Basically, I want to add a new line after occurrence of Wf. After the line Wf... (5 Replies)
Discussion started by: MIA651
5 Replies

5. Shell Programming and Scripting

Adding numbers matching with words

Hi All, I have a file which looks like this: abc 1 abc 2 abc 3 abc 4 abc 5 bcd 1 bcd 3 bcd 3 bcd 5 cde 7 This file is just a miniature version of what I really have. Original file is some 1 million lines long. I have tried to come up with the code for what I wish to accomplish... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Shell Programming and Scripting

Adding total of first field for each number in the second field

Dears, I need a script or command which can find the unique number from the second filed and against that number it adds the total of first field . 17215630 , 0 907043 ,1 201050 ,10 394149 ,4 1964 ,9 17215630, 0 907043 ,1 201050, 10 394149 ,4 1964 ,9 1234234, 55 23 ,100 33 ,67 ... (2 Replies)
Discussion started by: shary
2 Replies

7. Shell Programming and Scripting

Adding new field

Hello, I have a main file with IP addresses like this: Erisim var,100,172.17.241.5,4006,60,IS0799,TCP/IP Erisim var,1003,172.17.140.4,4004,60,IS2156,TCP/IP Erisim var,1004,172.17.140.5,4002,60,IS2636,TCP/IP Erisim var,1005,172.17.140.5,4004,60,IS2436,TCP/IP Erisim... (8 Replies)
Discussion started by: Spunkerspawn
8 Replies

8. Shell Programming and Scripting

Field matching between 2 files

Okay so I'm pretty new to scripting therefore this problem seems pretty tough. I have a main file that has a column of IP addresses and I have to compare it with 3 separate files that also have IP address columns. These 3 files are automatically generated from 3 different servers. Each time... (2 Replies)
Discussion started by: Spunkerspawn
2 Replies

9. Shell Programming and Scripting

Need some help matching a field in one file with a field in another

One file (file1) is as such: Abc.txt 1.1 1.3 Abc_v2.txt 1.1 1.4 Tree.txt 1.3 1.4 Grass.txt 1.3 4.5 The other (file2): Horse.txt 1.1 ref23232 Abc.txt 1.1 1.2 ref 3232-3232 1.3 ref 3232-3232 Plane.txt 1.1 1.2 ref 3232-3232 1.3 ref 3232-3232 1.4 Tree.txt 1.3 ref 3232-3232 1.4... (5 Replies)
Discussion started by: linuxkid
5 Replies

10. Shell Programming and Scripting

adding field values if field matches

hi i have file as below , i want to add duplicate records like bell_bb to one record with valuve as 15 ( addition of both ) any oneline awk script to achive this ? header 0 CAMPAIGN_NAME 1 Bell_BB 14 Bell_MONTHLY 803 SOLO_UNBEATABLE 644 Bell_BB 1 Bell_MONTHLY 25 SOLO_UNBEATABLE... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies
Login or Register to Ask a Question