http://www.perlmonks.org?node_id=98208

natty-dread has asked for the wisdom of the Perl Monks concerning the following question:

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

Originally posted as a Categorized Question.

  • Comment on how can i search a text file for a string and print every occurence of that string

Replies are listed 'Best First'.
Re: how can i search a text file for a string and print every occurence of that string
by davido (Cardinal) on Oct 03, 2003 at 02:33 UTC
    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):

    my @list = grep /\btest\b/, <DATA>; chomp @list; print "$_\n" foreach @list; __DATA__ This is only a test. Here's a line that doesn't contain the trigger text. This line contains test and test again. Now you see it, now you don't. Testing one two three. Test or not to test?

    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.

Re: how can i search a text file for a string and print every occurence of that string
by Agermain (Scribe) on Jul 20, 2001 at 00:31 UTC

    Not sure what you mean by 'printing every occurrence of the string' - I'm guessing you mean print the string in context. The snippet below ought to work - replace 'string to search for' and 'searchfile.txt' with the obvious. It will show the first 25 characters on either side of the search string.

    my $string = quotemeta 'string to search for'; my $slurp; { local $/ = undef; open my $textfile, '<', 'searchfile.txt' or die $!; $slurp = <$textfile>; close $textfile; } while( $slurp =~ m/ ( .{0,25} $string.{0,25} )gisx / ) { print "Found $1\n"; }
Re: how can i search a text file for a string and print every occurence of that string
by Hofmator (Curate) on Jul 20, 2001 at 16:44 UTC

    I normally wouldn't program that by hand. Depending on your operating system I would use a grep variant (on *nix) or find/findstr (on Win). Alternatively you can use the equivalent from the Perl Power Tools .

    --Hofmator

Re: how can i search a text file for a string and print every occurence of that string
by dkubb (Deacon) on Jul 22, 2001 at 02:49 UTC

    This will match each occurence of a string inside a file:

    #!/usr/bin/perl -w use strict; use IO::File; use constant FILE => 'search.txt'; use constant FIND => 'string to find'; IO::File->input_record_separator(FIND); my $fh = IO::File->new(FILE, O_RDONLY) or die 'Could not open file ', FILE, ": $!"; $fh->getline; #fast forward to the first match #print each occurence in the file print IO::File->input_record_separator while $fh->getline; $fh->close;

    Explanation: The input record seperator, $/ has been set to your search string. When perl reads the file line by line, it actually is scanning the file until it finds the search string, printing the string each time it finds it. To my knowledge this is the fastest way to do brute force exact text matches in a file with pure perl.

Re: 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
    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.