counts for every 1000 interval


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting counts for every 1000 interval
# 1  
Old 12-02-2011
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.

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

Thanks,
# 2  
Old 12-02-2011
Code:
#!/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}"

# 3  
Old 12-03-2011
Hi,

Somehow the code does not work

i/p
Code:
12
200
400
750
1000
1500
1800
2200
2345
2600
2896
3020
3400
3689
3977

o/p
Code:
1-1000  5
1001-2000   2
2001-3000   4
3001-4000   4

Thanks,
# 4  
Old 12-03-2011
Code:
$
$
$ 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
# 5  
Old 12-04-2011
Code:
#! /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;

# 6  
Old 12-04-2011
Code:
awk '
   {cnt[int($1/1000)]++ }
END {
         for (i in cnt) {
                printf "%4d-%4d %6d \n",i*1000,(i+1)*1000-1,cnt[i]
                }
         }
' inputfile | sort -t "-" -k 1,1

# 7  
Old 12-04-2011
Another one...
Code:
awk '{a[$0%1000?int($0/1000):int($0/1000)-1]++}END{for(i in a){print i*1000+1"-"(i+1)*1000,a[i]}}' input_file

If solaris, use nawk!

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counts not matching in file

I can not figure out why there are 56,548 unique entries in test.bed. However, perl and awk see only 56,543 and that # is what my analysis see's as well. What happened to the 5 missing? Thank you :). The file is attached as well. cmccabe@DTV-A5211QLM:~/Desktop/NGS/bed/bedtools$wc -l... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. UNIX for Dummies Questions & Answers

Get counts from List

This is very easy , but I`m struggling .. please help modify my script, I want to count the number of h and n , from the second column group by the first. The second column is binary, can only have h and n. a h a h a n a n a h b h b h b h b h b h c n c h c h c h c h (2 Replies)
Discussion started by: jianp83
2 Replies

3. Shell Programming and Scripting

compare the interval of 2 numbers of input2with interval of several numbers of input1

Help plz Does any one have any idea how to compare interval ranges of 2 files. finding 1-4 (1,2,3,4) of input2 in input1 of same key "a" values (5-10, 30-40, 45-60, 80-90, 100-120 ). Obviously 1-4 is not one of the range with in input1 a. so it should give out of range. finding 30-33(31,32,33)... (1 Reply)
Discussion started by: repinementer
1 Replies

4. UNIX for Dummies Questions & Answers

counts

To start I have a table that has ticketholders. Each ticket holder has a unique number and each ticket holder is associated to a so called household number. You can have multiple guests w/i a household. I would like to create 3 flags (form a, for a household that has 1-4 gst) form b 5-8 gsts... (3 Replies)
Discussion started by: sbr262
3 Replies

5. Solaris

file size counts??

Hello experts, I do - $ ls -lhtr logs2007* Is it possible that i can get the results of- totals size in MB/KB for ALL "logs2007*" note: in the same directory I have "logs2006*" & "logs2007*" files. (4 Replies)
Discussion started by: thepurple
4 Replies

6. Shell Programming and Scripting

to grep and print their counts

suppose u have a file ACFCFACCACARCSHFARCVJVASTVAJFTVAJVGHBAJ another file A C F R then output shud be A= 9 C=7 F=3 R=2 Thanks (12 Replies)
Discussion started by: cdfd123
12 Replies

7. UNIX for Advanced & Expert Users

Group by columns and get the counts

Hi Gurus, I have a file 1|usa|hh 2|usa|ll 3|usa|vg 4|usa|vg 5|usa|vg 6|usa|vg 7|usa|ll 8|uk|nn 9|uk|bb 10|uk|bb 11|kuwait|mm 12|kuwait|jkj 13|kuwait|mm 14|dubai|hh I want to group by last two columns and get the last two recs and count. (3 Replies)
Discussion started by: sumeet
3 Replies

8. UNIX for Dummies Questions & Answers

counts

How can i do a simple record count in my shell script? i just want to count the number of records i receive from a specific file. (11 Replies)
Discussion started by: k@ssidy
11 Replies
Login or Register to Ask a Question