Create range of values and count values.

Gents,

It is possible to generate a range of values according to column 1 and count the total of rows in the range.

example

input

15.3
15.5
15.8
15.9
16.0
16.1
16.8
17.0
17.5
18.0

output desired

15.0 - 15.9 = 4
16.0 - 16.9 = 3
17.0 - 17.9 = 2
18.0 - 18.9 = 1

Thanks for your help.

Yes. It is possible.

What operating system are you using?

What shell are you using?

What have you tried to solve this problem?

1 Like

Please, give it a try.

perl -nle '++$r{int($_)}; END{for(sort keys %r){print "$_.0 - $_.9 = $r{$_}"}}' jiam912.input

Output:

15.0 - 15.9 = 4
16.0 - 16.9 = 3
17.0 - 17.9 = 2
18.0 - 18.9 = 1
1 Like

Hi Don
The OS Debian 7
The Shell /bin/bash

I try

awk '{a[$1]++}END{for(i in a) print i, a }' file

It is only to count..

---------- Post updated at 08:18 PM ---------- Previous update was at 08:16 PM ----------

Aia

Thanks a lot

---------- Post updated at 08:18 PM ---------- Previous update was at 08:18 PM ----------

Aia

Thanks a lot

---------- Post updated at 08:18 PM ---------- Previous update was at 08:18 PM ----------

Aia

Thanks a lot

---------- Post updated at 08:18 PM ---------- Previous update was at 08:18 PM ----------

Aia

Thanks a lot

---------- Post updated at 08:18 PM ---------- Previous update was at 08:18 PM ----------

Aia

Thanks a lot

---------- Post updated at 08:18 PM ---------- Previous update was at 08:18 PM ----------

Aia

Thanks a lot

With a bit of modification to yours:

awk '{a[int($1)]++}END{for(i in a) print i".0 - "i".9 =", a}' input.file | sort -n
1 Like

Aia.. Thanks a lot for your help

---------- Post updated at 05:38 AM ---------- Previous update was at 12:19 AM ----------

Please can you modify a little the code to use this input.

1 15.3
1 15.5
2 15.8
2 15.9
3 16.0
3 16.1
4 16.8
4 17.0
5 17.5
6 18.0
1 18.0
10 17.1

to get this output.

       15.0 - 15.9   16.0 - 16.9   17.0 - 17.9   18.0 - 18.9 
1           2             0             0             1
2           2             0             0             0
3           0             2             0             0
4           0             2             0             0
5           0             0             1             0
6           0             2             0             1
10          0             0             1             0

Please if is possible to use awk..

Appreciate your help

Hint: use a two-dimensional array to store the data, and two nested loops to print them.

1 Like

RudiC,
I will try

Hi, I am trying to use this code to solve the problem but it does not work. :frowning:

awk '{if(!X[$1]++){FIR=FIR?FIR"_"$2:$2} {Y[$1]=$1;Z[$2]++}}END{n=split(FIR,FCL,"_");for(i=1;i<=n;i++){s=s?s"\t"FCL:"\t"FCL} print s; for(j in Z){s=j;for(i=1;i<=n;i++){split(FCL,PK," ");p=Y[PK[1],PK[2],j]?Y[PK[1],PK[2],j]:0;s=s"\t\t"p};print s}}' 

---------- Post updated at 06:51 AM ---------- Previous update was at 06:49 AM ----------

input

1 15.3
1 15.5
2 15.8
2 15.9
3 16.0
3 16.1
4 16.8
4 17.0
5 17.5
6 18.0
1 18.0
10 17.1

output desired

       15.0 - 15.9   16.0 - 16.9   17.0 - 17.9   18.0 - 18.9 
1           2             0             0             1
2           2             0             0             0
3           0             2             0             0
4           0             2             0             0
5           0             0             1             0
6           0             2             0             1
10          0             0             1             0

Hmmm - try

awk '
        {A[$1]
         B[int($2)]
         C[$1,int($2)]++
        }
END     {for (b in B) printf "\t%d.0 - %d.9", b, b
         printf ORS
         for (a in A)   {printf "%s", a
                         for (b in B) printf "\t%d", C[a,b]
                         printf ORS
                        }
        }
'  file
	15.0 - 15.9	16.0 - 16.9	17.0 - 17.9	18.0 - 18.9
10	0	0	1	0
1	2	0	0	1
2	2	0	0	0
3	0	2	0	0
4	0	1	1	0
5	0	0	1	0
6	0	0	0	1
1 Like

Thanks a lot RudiC