in reply to Grep lines from a text file, compare to an array using regex?
Or ( ... without grep, either OS-native nor Perl ...and definitely not to deprecate either suggestion above... ) you could go with a pair of arrays, this way:
#!/usr/bin/perl -w use 5.016; use strict; # 1089209.pl my @int_descriptions; # OP suggests the seeker knows how to + obtain the array # so, here's a test case without anno +ying the router @int_descriptions = ( 'foo.baz 12.247.62.7 20140608:1629', 'royal.com 1.11.617.251 20140608:1457', 'harvard.edu 1.1.217.0 20140608:1454', 'smithbarney.fin 195.7.168.2 20140608:1454', 'pomono.edu 93.16.124.5', 'yale.edu 58.3.179.6', 'etc.org 9.11.14.0 20140608:1320', ); open my $fh, '<', 'foo1089209.txt' or die "Can't open file for read, $ +!"; my @foo = <$fh>; for my $foo(@foo) { chomp $foo; say " *** Testing for matches to \$foo: $foo ***"; for (@int_descriptions) { my $int = qr/$_/; if ( $int =~ /$foo/ ) { say "\t \$foo: $foo found in \$int: $int"; } } # next; }
where foo1089209.txt looks like this:
my test file foo.bar pomona.edu wesleyan.edu harvard.edu yale.edu mit.edu oberline.edu foo.com bar.org smithbarney.fin ebay.co ebay.com
Which give you, as output:
*** Testing for matches to $foo: my test file foo.bar *** *** Testing for matches to $foo: pomona.edu *** *** Testing for matches to $foo: wesleyan.edu *** *** Testing for matches to $foo: harvard.edu *** $foo: harvard.edu found in $int: (?^u:harvard.edu 1.1.217.0 2 +0140608:1454) *** Testing for matches to $foo: yale.edu *** $foo: yale.edu found in $int: (?^u:yale.edu 58.3.179.6) *** Testing for matches to $foo: mit.edu *** *** Testing for matches to $foo: oberline.edu *** *** Testing for matches to $foo: foo.com *** *** Testing for matches to $foo: bar.org *** *** Testing for matches to $foo: smithbarney.fin *** $foo: smithbarney.fin found in $int: (?^u:smithbarney.fin 195 +.7.168.2 20140608:1454) *** Testing for matches to $foo: ebay.co *** *** Testing for matches to $foo: ebay.com ***
But -- that's not the most elegant of solutions.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Grep lines from a text file, compare to an array using regex?
by chaoticbear (Initiate) on Jun 09, 2014 at 19:01 UTC |