counts for every 1000 interval

Hi,

I have a file with 4 million rows. Each row has certain number ranging between 1 to 30733090.

What I want is to count the rows between each 1000 intervals.

1-1000  4000
1001-2000  2469
...
...
...
...
last 1000 interval

Thanks,

#!/usr/bin/ksh
typeset -i mCnt=0
typeset -i mFrom=1
typeset -i mTo=1000
while read mNbr; do
  while [[ ${mTo} -lt ${mNbr} ]]; do
    echo "${mFrom}-${mTo} ${mCnt}"
    mFrom=${mTo}+1
    mTo=${mTo}+1000
    mCnt=0
  done
  mCnt=${mCnt}+1
done < Inp_File
echo "${mFrom}-${mTo} ${mCnt}"

Hi,

Somehow the code does not work

i/p

12
200
400
750
1000
1500
1800
2200
2345
2600
2896
3020
3400
3689
3977

o/p

1-1000  5
1001-2000   2
2001-3000   4
3001-4000   4

Thanks,

$
$
$ cat f25
12
200
400
750
1000
1500
1800
2200
2345
2600
2896
3020
3400
3689
3977
$
$
$ perl -MPOSIX -ne '$x{ceil($_/1000)}++; END {foreach $k (sort keys %x){printf("%5d - %5d  %5d\n",(($k-1)*1000+1),($k*1000),$x{$k})}}' f25
    1 -  1000      5
 1001 -  2000      2
 2001 -  3000      4
 3001 -  4000      4
$
$
$

tyler_durden

#! /usr/bin/perl
use warnings;
use strict;

my ($line, %count);
my $interval = 1000;
open INPUT, "< source.txt";
for $line (<INPUT>) {
X: if ($line <= $interval) {
        $count{$interval}++
    }
    elsif ($line >= $interval) {
        $interval += 1000;
        goto X;
    }
}

for (sort {$a <=> $b} keys %count) {
    print $_-999 . "-$_ $count{$_}\n";
}
close INPUT;
awk '
   {cnt[int($1/1000)]++ }
END {
         for (i in cnt) {
                printf "%4d-%4d %6d \n",i*1000,(i+1)*1000-1,cnt
                }
         }
' inputfile | sort -t "-" -k 1,1

Another one...

awk '{a[$0%1000?int($0/1000):int($0/1000)-1]++}END{for(i in a){print i*1000+1"-"(i+1)*1000,a}}' input_file

If solaris, use nawk!

--ahamed

Please, before you say a code does not work, pay close attention to what you are saying.

In any situation in life, always be sure on your statements.

The code DOES work according to your specifications.