Debian Codegraph @

numgrep

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