Eric Plowe wrote:
>
> Hello all.
Hello,
> I am working on a script that searches through a text file - each
> 'record' within the text file is separated by a - so for example:
>
> -
> Fancy Car
>
> text text
> text text
> -
>
> and so on and so on.
>
> the first initial row of text is what I am searching for - then I can
> handle parsing the rest of the text after that to the next delimiter.
>
> But right now with the script.. its really basic, you can pass a search
> argument to it and it'll return results. but I need to narrow it down.
> because it may return data I didnt search for. but it match to some
> extent the input. Here's the script as I have it now.
>
> --
> #!/usr/bin/perl -w
>
> use strict;
>
> my $filename = "somefile";
> my $bit = 0;
> my $search = $ARGV[0];
>
> open(FILE,$filename) or die ("Couldn't open file - !$\n");
>
> while (<FILE>) {
> chomp;
> if ($_ =~ /^-/) {
> $bit = 2;
> next;
> }
>
> if ($_ =~ /^$search*/) {
> $bit = 1;
> }
>
> if ($bit == 1) {
> print "$_\n";
> }
> }
> close(FILE);
> --
>
> the bit variables are there to basically do this: If the first line is
> a - (the record delimiter) then set $bit to 2 and scroll to the next
> line. If the line matches the inputed search - then set bit to 1 - so
> then now as it scrolls through the while, as long as $bit hasn't been
> changed to 2 - it'll print out each line until it hits the next '-'
>
> Now my question for the perl gurus is - How to optimize the regex (or
> lack of) I am using to match my inputed search? all I want to test
> against is the next line after a '-' and still attempt to match it even
> if its not the exact word (ie: typing in 'off' it would match off,
> office, Office, OFFICE, OFF, fell OFF the bike... i think you get my
> point) Thanks! and I hoped I explained my question/problem clearly.
What I would do is set the input record separator.
#!/usr/bin/perl -w
use strict;
my $filename = 'somefile';
my $search = shift or die "Error: search string required!\n";
$/ = "-\n"; # set input record separator
open FILE, $filename or die "Couldn't open $filename: $!";
while ( <FILE> ) {
chomp;
if ( /^$search/ ) {
print;
}
}
close FILE;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]