Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by munchie (Monk)
on May 17, 2002 at 14:01 UTC ( #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 - Answer 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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2016-12-11 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (169 votes). Check out past polls.