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 added together to obtain the sum. I've tried a lot of commands I found on the forum (mainly using awk) and tried piecing my own answer together but can't seem to quite get it. I have One file that looks equivalent to:

ID#,Date,Minutes
100,5/20/2013,22
101,7/21/2013,33
101,7/21/2013,73
101,7/21/2013,73
101,7/23/2013,26
102,7/24/2013,43

The net result I'd like to achieve is:

ID#,Date,Minutes
100,5/20/2013,22
101,7/21/2013,179
101,7/23/2013,26
102,7/24/2013,43

I have two separate Perl scripts which take different files, chops up the columns from those files and combines them to this one file, but I don't know where to go from here to achieve the results I'm looking for. The closest command I've come across is:

awk '{last=$2}{if(last == $3) getline;print}' f.csv

Which sounds KIND of like what I'm trying to do, to compare the rows/columns to each other and print the results, but I don't understand how to produce the sum if the condition is true.

Is using awk even appropriate? I sure would appreciate any help to go in the right direction.

Try this:

awk -F, 'NR==1; NR>1 {Arr[$1","$2]+=$3} END{for (i in Arr) print i","Arr}' file
ID#,Date,Minutes
100,5/20/2013,22
102,7/24/2013,43
101,7/23/2013,26
101,7/21/2013,179
1 Like

Rudi!!! You are awesome, that is exactly what I was looking for. If I may, can I ask a question about the line?

I know from researching the little I do know about "awk" that:

-F tells the field separator
NR is number of rows

But I does the rest of the statement work? Sorry, I know it's trivial, but I do like to learn (and already have learned a lot from scanning the forums even :o)

Thank you again!

This may change the order of the lines

awk -F, '{a[$1","$2]+=$3} END {for (i in a) print i","a}'
100,5/20/2013,22
102,7/24/2013,43
101,7/23/2013,26
101,7/21/2013,179

Edit: Hmm, this was nearly the same as Rudic posted.

1 Like

Great, thanks Jotne, I sure appreciate your answer as well! I don't mind about the rows being rearranged... the "sort" command is absolutely divine!!

---------- Post updated at 09:27 AM ---------- Previous update was at 09:20 AM ----------

Is there a way I can change the topic of this thread to something like "[ANSWERED]" so that others can see this thread was answered when scanning the forums?

awk -F, 'NR==1;                                 # print line #1 as is (pattern is true; print is default action)
         NR>1 {Arr[$1","$2]+=$3}                # create (or use if exists) array indexed by $1,$2 and sum $3 into it
         END{for (i in Arr) print i","Arr}   # At EOF, run i through the indices of array, print it, and the array element poited to by i
        ' file
1 Like

Rudi, thanks for taking your time to explain that. I sure appreciate it.