* read text file/stdin and check each line with multiple regex
[lab.git] / misc / regexfilter.pl
1 #! /usr/bin/perl -w
2
3 use warnings;
4 use strict;
5
6 use Regexp::Assemble;
7
8 if ($#ARGV + 1 < 1) {
9     print "$0: list_of_regex [filter_target]\n";
10     exit 0;
11 }
12
13 my $regexfile = shift @ARGV;
14 if (! -r $regexfile) {
15     warn "$0: cannot read '$regexfile'";
16     exit 1;
17 }
18
19 my $re = Regexp::Assemble->new(file => "$regexfile");
20
21 my $target = shift @ARGV;
22
23 my $input;
24 unless (defined($target) and -f $target) {
25     print STDERR "read from STDIN\n";
26     $input = *STDIN;
27 }
28 else {
29     open $input, "<$target";
30 }
31
32 my $line;
33 while ($line = <$input>) {
34     chomp $line;
35     if ($re->match($line)) {
36         print "OK: ";
37     }
38     else {
39         print "NG: ";
40     }
41     print $line, "\n";
42 }