windya
1
Hi friends,
I have a text file as follows
X 20
Y 60
X 40
U 30
X 10
U 20
I want to get an out put like follow
X 70
Y 60
U 50
It should group the name and add the values together..
Please tell me how to do this using awk..
Thanks in advance..
Windi
Try this :
########### sum.awk #################
BEGIN{
FS=" ";
}
{
key=$1;
sum2[key]+=$2;
}
END{ for (word in sum2)
printf("%s %s\n",word,sum2[word]);
}
########### sum.awk #################
windya
3
Hi friends,
I have a text file as follows
X 20
Y 60
X 40
U 30
X 10
U 20
I want to get an out put like follow
X 70
Y 60
U 50
It should group the name and add the values together..
Please tell me how to do this using awk..
Thanks in advance..
Windi
Edit/Delete Message
nazeeb
4
something like this may help
awk'
BEGIN{FS=" "}
/X/ { x_count+= $2 }
/Y/ { y_count+= $2 }
/Z/ { z_count+= $2 }
END {
printf ("X %d\n",x_count);
printf ("Y %d\n",y_count);
printf ("Z %d\n",z_count);
}' inputfile
jaduks
5
$ awk '{arr[$1]+=$2} END {for (i in arr) {print i,arr}}' win.txt
U 50
X 70
Y 60
windya
6
Hiii
Thanks a lot..................................
windi