Count occurences of a numeric string falling in a range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count occurences of a numeric string falling in a range
# 1  
Old 06-30-2011
Count occurences of a numeric string falling in a range

Dear all,

I have numerous dat files (1.dat, 2.dat...) containing 500 numeric values each. I would like to count them, based on their range and obtain a histogram or a counter.



INPUT:

1.dat
1.3
2.16
0.34
......

2.dat
1.54
0.94
3.13
.....

3.dat
2.34
4.12
2.7
.....


OUTPUT:
example-
1.dat: low (2>) - 200 ; medium (3> and 2<) - 50 ; high (3<) -250

(the output can be in any comprehensive format)

I used the following script, in vain!


Code:

#!/bin/csh
set k = 1
while ($k = 250)

set i=0
set low = 0
set medium = 0
set high = 0
foreach line ("`cat $k.dat`")
set j = `printf $line`
if ($j < 2) then
@low = $low +1
else if ($j > 2 and <3) then
@medium = $medium +1
else if ($j > 3) then
@high = $high +1
else
endif
end

echo "$k - $low $medium $high"
@k = $k +1
end

I get an error sayin "if: Badly formed number.
Could you please help?


Thanks,
Po
# 2  
Old 06-30-2011
Place this script in the directory containing .dat files, then run it there.
Code:
#!/usr/bin/perl
chomp(@files=`ls *.dat`);
for $i (@files){
  open I, "$i";
  $l=$m=$h=0;
  while ($n=<I>){
    chomp $n;
    $l++ if $n<=2;
    $m++ if $n>2 && $n<=3;
    $h++ if $n>3;
  } 
  print "$i: low (2>) - $l ; medium (3> and 2<) - $m ; high (3<) - $h\n";
}

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-30-2011
Thanks Bartus11
Smilie
# 4  
Old 06-30-2011
Hi,

Other solution using 'perl' too:
Code:
$ cat script.pl
use warnings;
use strict;

die "Usage: perl $0 low-range medium-range high-range files\n" unless 
    @ARGV >= 4 and
    grep { /^-?\d+$/ } @ARGV[0..2] and    # Integer ranges.
    defined $ARGV[3];                    # At least one file.

my %range = (
    low_limit => shift,
    medium_limit => shift,
    high_limit => shift
);


while ( <> ) {
    ## Only count lines without number.
    if ( /^\s*\d+(?:\.\d*)\s*$/ ) {
        if ( $_ < $range{ low_limit } ) {
            $range{ low_num }++;    
        } elsif ( $_ >= $range{ low_limit } && $_ < $range{ medium_limit } ) {
            $range { medium_num }++;
        } elsif ( $_ >= $range{ high_limit } ) {
            $range{ high_num }++;
        }
    }

    ## Print result in end of each file.
    if ( eof ) {
        printf 
            "%s: low (%d<) - %d ; medium (>%d and %d<) - %d ; high (>%d) - %d\n", 
            $ARGV,
            $range{ low_limit },
            $range{ low_num } || 0,
            $range{ low_limit },
            $range{ medium_limit },
            $range{ medium_num } || 0,
            $range{ high_limit },
            $range{ high_num } || 0,
            delete @range{ qw(low_num medium_num high_num) }; 
    }
}
$ perl script.pl
Usage: perl script.pl low-range medium-range high-range files
$ perl script.pl 2 3 3 *.dat  # Output with my test files.
1.dat: low (2<) - 4 ; medium (>2 and 3<) - 1 ; high (>3) - 1
2.dat: low (2<) - 2 ; medium (>2 and 3<) - 1 ; high (>3) - 3

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count the occurences of strings

I have some text files in a folder f1 with 10 columns. The first five columns of a file are shown below. aab abb 263-455 263 455 aab abb 263-455 263 455 aab abb 263-455 263 455 bbb abb 26-455 26 455 bbb abb 26-455 26 455 bbb aka 264-266 264 266 bga bga 230-232 230 ... (10 Replies)
Discussion started by: gomez
10 Replies

2. Shell Programming and Scripting

Delete lines falling in a range

Hi All, I have a file which contains lakhs of records 0136812368126 03000 Statement 1237129372189 02321 JIT 0136812368126 05000 terminal 1237129372189 05001 Utilise Is there an option to delete all lines which fall within the range 05000 to 05999? I tried... (6 Replies)
Discussion started by: swasid
6 Replies

3. Shell Programming and Scripting

awk count occurences

line number:status, market, keystation 1,SENT,EBS,1 : 1 2,DONE,REU,1 : 1 3,SENT,EBS,2 : 1 4,DONE,EBS,1 : 0 5,SENT,EBS,2 : 0 6,SENT,EBS,2 : 0 7,SENT,EBS,2 : 0 8,SENT,EBS,1 : 1 for each status, market combination I want to keep a tally of active orders. i.e if an order is SENT, then +1, if... (8 Replies)
Discussion started by: Calypso
8 Replies

4. UNIX for Dummies Questions & Answers

Count pattern occurences

hi, I have a text..and i need to find a pattern in the text and count to the no of times the pattern occured. i have used grep command ..but the problem is , it shows the occurrences of the pattern but doesn't count no of times the pattern occuries. (5 Replies)
Discussion started by: nvnni
5 Replies

5. Shell Programming and Scripting

Count occurences of string

Hi, Please help me in finding the number of occurences of the string. Example: Apple, green, blue, Apple, Orange, green, blue are the strings can be even in the next line. The o/p should look as: Word Count ----- ----- Apple 2 green 2 Orange 1 blue 2 Thanks (2 Replies)
Discussion started by: acc888
2 Replies

6. Shell Programming and Scripting

Awk to count occurences

Hi, i am in need of an awk script to accomplish the following: Input table looks like: Student1 arts Student2 science Student3 arts Student4 science Student5 science Student6 science Student7 science Student8 science Student9 science Student10 science Student11 science... (8 Replies)
Discussion started by: saint2006
8 Replies

7. Shell Programming and Scripting

Perl - Count occurences

I have enclosed the script. I am able to find the files that contain my search string but when I try to count the occurences within the file I get zero always. Any help on this. #!/usr/bin/perl my $find = $ARGV; my $replace = $ARGV; my $glob = $ARGV; @filelist = <*$glob>; # process each... (22 Replies)
Discussion started by: TimHortons
22 Replies

8. Shell Programming and Scripting

How to count the number of occurences of this pattern?

Hi all, I have a pattern like this in a file: 123 4 56 789 234 5 67 789 121 3 56 789 222 4 65 789 321 6 90 100 478 8 40 789 243 7 80 789 How can I count the number of occurences of '789' (4th column) in this set...? Thanks for all your help! K (7 Replies)
Discussion started by: kripssmart
7 Replies

9. Shell Programming and Scripting

numeric range comparisons

I have two files.And a sort of matrix analysis. Both files have a string followed by two numbers: File 1: A 2 7 B 3 11 C 5 10 ...... File 2: X 1 10 Y 3 5 Z 5 9 What I'd like to do is for each set of numbers in the second file indicate if the first or second number (or both) in... (7 Replies)
Discussion started by: dcfargo
7 Replies

10. UNIX for Advanced & Expert Users

How to count no of occurences of a character in a string in UNIX

i have a string like echo "a|b|c" . i want to count the | symbols in this string . how to do this .plz tell the command (11 Replies)
Discussion started by: kamesh83
11 Replies
Login or Register to Ask a Question