Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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 having an uproarious good time at the Monastery: (8)
As of 2014-12-25 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls