Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Answer: how can i search a text file for a string and print every occurence of that string

( #167301=categorized answer: print w/ replies, xml ) Need Help??

Q&A > strings > how can i search a text file for a string and print every occurence of that string contributed by munchie

Here's a less compicated way to do it.

use strict; my $find = "word or string to find"; open FILE, "<searchfile.txt"; my @line = <FILE>; print "Lined that matched $find\n"; for (@lines) { if ($_ =~ /$find/) { print "$_\n"; } }
However with this will not pick up a match that occurs over multiple lines, and it also is not very fast when munging through huge files.

Comment on Answer: how can i search a text file for a string and print every occurence of that string
Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-07-30 09:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (230 votes), past polls