> >---- Original Message ---
> >From: Jorge Goncalvez <[EMAIL PROTECTED]>
> >To: [EMAIL PROTECTED]
> >Cc:
> >Subject: Re:seek a string
> >
> >Hi, I would like to open a file and know if a string exists.
> >How can I do this?
> >Thanks
--- Nestor Dutko <[EMAIL PROTECTED]> wrote:
> How about:
>
> my $cmd='grep string file';
> unless (defined $cmd) {
> die "string not found";
> }
>
There are a few problems with that.
1. It requires backticks, not single quotes. Of course, perhaps that is simply a bit
of munging
done by an email client somewhere.
2. It's not cross-platform compatible. What if Jorge Goncalvez is using Windows?
3. It could be horrible insecure, depending upon how the string and filename is
passed to the
command.
If string your looking for is broken over lines or the string you're trying to match
has an
embedded newline, the solution that I put forward is not going to work:
my $string = quotemeta "**test**";
my $line = 0;
open INFILE, "< $infile" or die "Cannot open $infile for reading: $!";
while(<INFILE>) {
$line = $., last if /$string/;
}
close INFILE;
If the string "**test**" (without the quotes) is in the file you're reading, then
$line will be
set to the first line number the string is on. Otherwise, $line will be zero.
This may not meet your exact needs, but it should give you an idea of where to start.
Note that $. is the current input line number of the last file read.
Cheers,
Curtis "Ovid" Poe
=====
Senior Programmer
Onsite! Technology (http://www.onsitetech.com/)
"Ovid" on http://www.perlmonks.org/
__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]