Hi,
I have problem in writing an awk script.
I would like to find out unique value of column 2.
input:
1 baby 25.6
2 flower 33.6
3 cake 33.4
4 baby 11.2
5 fruits 22.3
6 cake 66
Then for each value in the unique list, get the number of line.
output:
baby 2
flower 1
cake 2
baby 1
fruits 1
my awk program is,
#find the unique value
{a[$2]++}
{for (b in a) /b/ {i++;print i}}
Do you know where is my error?
Thanks!
awk '{a[$2]++}END {for (b in a) print b, a}' myFile
{ a[$2]++ }
END { for (i in a) print i,a
Jean-Pierre.
joeyg
4
C:\cygwin\tmp>cat pr.txt
1 baby 25.6
2 flower 33.6
3 cake 33.4
4 baby 11.2
5 fruits 22.3
6 cake 66
C:\cygwin\tmp>cut -d" " -f2 <pr.txt| sort | uniq -c | gawk '{print $2,$1}'
baby 2
cake 2
flower 1
fruits 1
What if I want to output the sum of column 3 for each unique value?
baby 36.8
flower 33.6
cake 99.4
fruits 22.3
Thanks!
awk '{a[$2]+=$3}END {for (b in a) print b, a}' myFile