Contributed by natty-dread
on Jul 19, 2001 at 23:36 UTC
Q&A
> strings
Answer: how can i search a text file for a string and print every occurence of that string contributed by Agermain 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";
}
| Answer: how can i search a text file for a string and print every occurence of that string contributed by Hofmator 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 | Answer: how can i search a text file for a string and print every occurence of that string contributed by dkubb 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. | Answer: how can i search a text file for a string and print every occurence of that string contributed by davido 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. | Answer: 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. |
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
Log In?
|
|
Chatterbox?
|
How do I use this? | Other CB clients
|
Other Users?
|
Others pondering the Monastery: (4) As of 2021-02-27 19:36 GMT
|
Sections?
|
|
Information?
|
|
Find Nodes?
|
|
Leftovers?
|
|
Voting Booth?
|
No recent polls found
|
Notices?
|
|
|