Column sum group by uniq records

Dear All,

I want to get help for below case.
I have a file like this.

saman 1
gihan 2
saman 4
ravi 1
ravi 2

so i want to get the result,

saman 5
gihan 2
ravi 3 like this.

Pls help me.

Thank you.

What have you tried so far?

This can be done using associative array in awk.


$ cat nayan.out
saman 1
gihan 2
saman 4
ravi 1
ravi 2

$ awk '{arr[$1]+=$2} END {for (i in arr) {print i,arr}}' nayan.out > nayan.out.tmp

$ cat nayan.out.tmp
ravi 3
saman 5
gihan 2

//Jadu

Dear Jadu,

Thank u ! it is working.

Thanks you again,

Nayanajith.

Hi Jadu,

I am new to unix and i have a similar requirement given below:

Input file:

Test.txt
PORT; ID; TOTAL
port1;p1;100000
port2;p2;5000
port1;p1;500

Output file:
PORT; ID; TOTAL
port1;p1; 100500
port2;p2; 5000

How can achive this? Any help on this regard is higly appreciated.
Thanks.

Regards,
Sandeep

Try below script ( Not tested)

awk -F ";" '{ arr[$1 ";" $2] += $3 } END {for (i in arr) {print i ";" arr } }' inputfile

Hi Ranjith,
Thanks for the reply but this doesn't work....can we use like arr[$1 ";" $2]???

Regards,
Sandeep

Try this

awk -F  ";"  '{ string=$1 ";" $2; arr[string] += $3 } END {for (i in arr) {print i ";" arr } }' inputfile

This one also doesn't work....

What doesn't work? Did you get errors, wrong output, no output? Try this:

awk 'BEGIN{FS=OFS=";"}
NR==1{print;next}
{a[$1";"$2]+=$3}
END{for(i in a)print i, a}' file

Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards

Hi Franklin,

I tried ur solution and getting below o/p

PORT;PID;TOTAL
port1;p1
port2;p2

I guess someting is missing, it is doing group by but missing the output values.
Thanks for the response.

Regards,
Sandeep

getting below o/p..missed some values in last post

PORT;PID;TOTAL
port1;p1;500
port2;p2;0

This is what I get:

$ cat file
PORT; ID; TOTAL
port1;p1;100000
port2;p2;5000
port1;p1;500
$
$
$ awk 'BEGIN{FS=OFS=";"}                              
NR==1{print;next}
{a[$1";"$2]+=$3}
END{for(i in a)print i, a}' file
PORT; ID; TOTAL
port2;p2;5000
port1;p1;100500
$
$

Regards

HI Franklin,

Thanks a lot. It works....using nawk

/usr/bin/nawk 'BEGIN{FS=OFS=";"}NR==1{print;next}{a[$1";"$2]+=$3}END{for(i in a)print i, a[i]}' file

Much appreciated.

Cheers,
Sandeep

Hi Guys,

Once again I have a query regarding grouping the columns, below is my requirement:

Input File:
COL1; COL2; AMT1;AMT2
PORT1;CURR1;100;50
PORT1;CURR1;200;100
PORT2;CURR2;300;150
PORT3;CURR3;400;200
PORT3;CURR3;500;250

Expected Output:
COL1; COL2; AMT1;AMT2
PORT1;CURR1;300;150
PORT2;CURR2;300;150
PORT3;CURR3;900;450

How can I pass to values in below command:
/usr/bin/nawk 'BEGIN{FS=OFS=";"}NR==1{print;next}{a[$1";"$2]+=$3}END{for(i in a)print i, a[i]}' <INPUT FILE>

Please suggest so that I can get both AMT1 and AMT2 gouped by COL1 and COL2.
Thanks in advance.

Regards,
Sandeep

nawk '
BEGIN { FS=OFS=";" }
NR==1 { print ; next }
{
   id = $1 ";" $2;
   amt1[id] += $3;
   amt2[id] += $4;
}
END {
   for (id in amt1)
      print id, amt1[id], amt2[id];
}
' inputfile

Jean-Pierre.

Or with a little adjustment of my solution:

awk 'BEGIN{FS=OFS=";"}                              
NR==1{print;next}
{a[$1";"$2]+=$3; b[$1";"$2]+=$4}
END{for(i in a)print i, a, b}' file

Thanks Aigles and Franklin.
I already got it work.

Regards,
Sandeep