Suppose i have this file
var1 300
var1 400
var3 600
var1 200
Now how can i compare the $1(from line 1) == $1 (from line 2)
basically i just want to add the columns if the name is equal
Only with awk
The output should br
var1 900
var3 600
Suppose i have this file
var1 300
var1 400
var3 600
var1 200
Now how can i compare the $1(from line 1) == $1 (from line 2)
basically i just want to add the columns if the name is equal
Only with awk
The output should br
var1 900
var3 600
Using awk:
$ awk '{a[$1]+=$2;}END{for (i in a)print i, a[i];}' file
var1 700
var3 600
Use this over @Guru's solution if your file is large and/or you care about preserving the input order and your input is sorted on the first field:
$ awk '(NR>1) && ($1!=p){print p, s; s=0} {p=$1; s+=$2} END{print p, s}' file
var1 700
var3 600