1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
#!/usr/bin/perl -w
# numgrep: This program is the numeric equivilent of the grep
# utility. It searches for numbers, sets of numbers and so on.
#
# Copyright (C) 2002-2004 Suso Banderas
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# You may contact the author at <suso@suso.org>.
#######################
# VARIABLES AND SETUP #
#######################
use Getopt::Std;
use strict;
use vars qw/ %opts $verbose /;
$| = 1;
getopts('hdVl', \%opts);
if ($opts{'h'}) {
&help;
exit(0);
}
if ($opts{'d'}) {
$verbose = 3;
print STDERR "Debug mode\n";
} elsif ($opts{'V'}) {
$verbose = 2;
print STDERR "Verbose mode\n";
} elsif($opts{'q'}) {
$verbose = 0; # No output except the final answer.
} else {
$verbose = 1; # Normal output.
}
my $match_expression = shift;
$match_expression =~ s/^\/(.*)\/$/$1/;
# Now read in any extra arguments as filenames to do searches in.
my @files = @ARGV;
print STDERR "Arg files: " . @files . "\n" if ($verbose >= 3);
my @ranges;
my $file;
################
# MAIN PROGRAM #
################
if (@ARGV) {
foreach $file (@ARGV) {
print STDERR "Reading from file $file.\n" if ($verbose >= 2);
open (ARGFILE, "$file") && process_filehandle(\*ARGFILE, $match_expression)
|| $verbose && warn "Couldn't open file $file for reading: $!\n";
close(ARGFILE);
}
} else {
print STDERR "Reading from STDIN.\n" if ($verbose >= 2);
process_filehandle(\*STDIN, $match_expression);
}
exit(0);
###############
# SUBROUTINES #
###############
sub help {
print <<"EOF";
-----------------------------------------------------------
numgrep: Search for a number using numeric expressions.
-----------------------------------------------------------
Usage:
numgrep [options] /expression/ file
| numgrep [options] /expression/
numgrep [options] /expression/
Options:
-l Print the matching numbers out one per line
instead of printing the entire line they are on.
-d Debug. For developers only.
-h Help: You're looking at it.
-V Increase verbosity.
Expressions:
.. Range. Use to specify a range of numbers such
as 1..10, 1.. or ..10. Which mean between 1 and 10, greater than
1, and less than 10 respectively
, Expression seperator. The comma seperates expressions within
the forward slashes. ie. /1..10,20..30,50,60,127/
m Multiple operator. Using the m followed by an integer will match
any multiple of that integer. So /m3/ would match 3, 6, 9, 12, etc.
f Factor operator. Using the f followed by an integer will match
any factor of that integer. So /m20/ would match 1, 2, 4, 5 and 10.
EOF
}
sub process_filehandle {
my $filehandle = shift;
my $returnvalue;
# The below array holds individual expressions that
# will be looped through to find matching values.
while (<$filehandle>) {
my $line = $_;
if ($line =~ /[0-9]/) { # This expression will determine whether we
# should even try to match. Maybe it's wrong
# to do this, but it will speed things up.
num_match($line);
} else {
next;
}
}
return 1;
}
# This subroutine determines what to do with matched lines.
sub num_match {
my $line = shift;
my $returnvalue = 0;
my $expression;
my $number;
my @expression_array = split(/,/, $match_expression);
my @numbers = ();
my $strlen = length($line);
my $n = 0;
while ($n < $strlen) {
my $char = substr($line, $n, 1);
if ($char =~ /[0-9\.-]/) {
($n, $number) = parse_number(substr($line, $n, ($strlen - $n)), $n);
unless ($number eq "") {
push (@numbers, $number);
}
} else {
$n++;
}
}
if ($opts{'l'}) { # Follow this branch if the user just wants the numbers and not the context.
foreach $number (@numbers) { # SET represents the numbers found on one line.
chomp($number);
EXP: foreach $expression (@expression_array) {
print "Expression: $expression\n" if ($verbose >= 3);
my $exp = make_expression($expression, $number);
# Now if the $exp is true then go on.
if (eval($exp)) {
print "$number\n";
last EXP;
}
}
}
} else { # Else we'll just print out lines that match one of the expressions.
NUM: foreach $number (@numbers) {
foreach $expression (@expression_array) {
my $exp = make_expression($expression, $number);
if (eval($exp)) {
print "$line";
last NUM;
}
}
}
}
return $returnvalue;
}
sub make_expression {
my $expression = shift;
my $number = shift;
my $exp;
if ($expression =~ /^(-?[0-9\.]+)\.\.(-?[0-9\.]+)$/) { # normal range between two numbers.
$exp = "$1 <= $number && $number <= $2";
} elsif ($expression =~ /^\.\.(-?[0-9\.]+)$/) { # Anything lower than X
$exp = "$number <= $1";
} elsif ($expression =~ /^(-?[0-9\.]+)\.\.$/) { # Anything higher than X
$exp = "$number >= $1";
} elsif ($expression =~ /^(-?[0-9\.]+)$/) {
$exp = "$number == $1";
} elsif ($expression =~ /^m(-?[0-9\.]+)$/) { # Multiples.
if ($1 =~ /\./) {
die "Please use only integer values for m.\n";
$exp = "$number =~ /$expression/";
} else {
$exp = "!($number % $1)";
}
} elsif ($expression =~ /^f(-?[0-9\.]+)$/) { # Factors.
if ($1 =~ /\./) {
die "Please use only integer values for f.\n";
$exp = "$number =~ /$expression/";
} else {
$exp = "!($1 % $number)";
}
} else {
$exp = "$number =~ /$expression/";
}
print "Exp: '$exp'\n" if ($verbose >= 3);
return $exp
}
sub parse_number {
my $substring = shift;
my $placeholder = shift;
$substring =~ /^(-?[0-9]*\.?[0-9]*)[^0-9]/;
my $number = $1;
my $strlen;
if ($number =~ /^[\.-]*$/) {
$strlen = length($number);
$number = "";
} else {
$strlen = length($number);
}
# Fix the number somewhat.
#$number =~ s/^\./0./;
#$number =~ s/^-\./-0./;
#$number =~ s/-0+/0/;
#$number =~ s/\.$//;
chomp($number);
return ($placeholder + $strlen, $number);
}
# Lay down some of that perl pod action.
=pod
=head1 NAME
numgrep - This program is the numeric equivilent of the grep utility.
=head1 SYNOPSIS
B<numgrep> [-dhlV] <FILE>
| B<numgrep> [-dhlV] (Input on STDIN from pipeline.)
B<numgrep> [-dhlV] (Input on STDIN. Use Ctrl-D to stop.)
=head1 DESCRIPTION
B<numgrep> searches for different occurances of numbers through the use
of numeric expressions.
=head1 OPTIONS
-l Print the matching numbers out one per line
instead of printing the entire line they are on.
-h Help: You're looking at it.
-V Increase verbosity.
-d Debug mode. For developers
=head1 EXPRESSIONS
B<numgrep> uses a special numeric expression matching system. Basically,
it searches for ranges, factors and sequences of numbers. Here is a list
of the syntax characters and some sample expressions that will get you
going:
/<expression>/
Put your expression or set of expressions between these
two forward slashes.
.. Range expression. A number must be used on the left
and/or right of this expression to specify that numbers
between, greater than or less than the numbers specified
should be matched.
, Expression sepeartor. The comma sepearates one complete
expression from another in a set enclosed by //.
m<n> Multiples of <n>. This operator, followed by a number
<n> will match any number <x> that is an integer
multiple of <n>. Meaning that <x> = <n> times <y>,
where <y> is any integer.
f<n> Factors of <n>. This operator, followed by a number <n>
will match any number <x> that is an integer factor of
<n>. Meaning that <x> = <n> divided by <y>, where <y>
is any integer.
B< NOTE:> Checking for factors and multiples is very fast because it
is checked by doing a single modulus operation on two numbers.
B<Examples:>
/2..10/ Match any number between 2 and 10.
/2..10,20..30/ Match any number between 2 and 10 or between 20 and 30.
/56,34,512,45,67/ Match any of the numbers 56, 34, 512, 45 or 67.
/m3/ Match any integer that is a multiple of 3.
/f1024/ Match any integer that is a factor of 1024.
$ numrange -N /1..1000/ | numgrep /f1024/
1
2
4
8
16
32
64
128
256
512
$
=head1 BUGS
B<numgrep> can't handle certain situations properly. Such as if
it encounters a number with leading zeros, it will treat it as an
octal number and thus might not match the way you would expect.
B<numgrep> does not yet allow you to mix numbers and text in the
matching expression. So you can not do something like
'numgrep /port=0..1023/ firewall.log'. But this will be changed in the
future.
=head1 SEE ALSO
numaverage(1), numbound(1), numinterval(1), numnormalize(1), numprocess(1), numsum(1), numrandom(1), numrange(1), numround(1)
=head1 COPYRIGHT
numgrep is part of the num-utils package, which is copyrighted by
Suso Banderas and released under the GPL license. Please read
the COPYING and LICENSE files that came with the num-utils package
Developers can read the GOALS file and contact me about providing
submitions or help for the project.
=head1 BUGS
numgrep will round decimal numbers with more than 15 digits of accuracy. This is
mostly due to limit's in the way programming languages deal directly with numbers.
=head1 MORE INFO
More info on numgrep can be found at:
=over 1
=item B<http://suso.suso.org/programs/num-utils/>
=back
=cut
|