Perl-Sensitive Sunglasses | |
PerlMonks |
Re: how can i search a text file for a string and print every occurence of that stringby davido (Cardinal) |
on Oct 03, 2003 at 02:33 UTC ( [id://296143]=note: print w/replies, xml ) | Need Help?? |
If the file is small enough that you don't mind a little slurping, here's an easy way using grep. (This assumes that you want to print the entire line where the matches are found):
This passes a filehandle to grep, along with a simple regexp that tells it to find only those lines that contain the word "test" (rejecting words like 'testing'). For matches across multiple lines you have to set the $/ special variable to paragraph or slurp mode and use an /s modifier on your regexp.
In Section
Seekers of Perl Wisdom
|
|