rizzy has asked for the wisdom of the Perl Monks concerning the following question:
I have been slurping the file to a string and using regular expressions to find a fixed number of characters (in this example 30) before and after like so:Here is my text file\n I want to save a bunch of\n charcaters before the keywords\n for example the keywords might be\n the phrase: these are my keywords\n I want to save a bunch of characters\n after the keywords too so I have\n context\n \n The keywords may appear multiple\n times in any given file and may\n span across lines like so: these are\n my keywords. This is one reason\n I was using slurp instead of reading\n in line by line
This will spit out something like this:#!C:/Perl/bin -w use File::Slurp; my $filetext= read_file("input.txt"); while($filetext=~ m{(.{30}(these\s+are\s+my\s+keywords).{30})}gis) { print "$1\n"; }
Is there a more efficient way to do this (i.e., save 200 characters before and after a keyphrase) than to read the entire file into an array? It seems like reading this in line by line will not allow me to pull characters before and after newlines very easily. A workaround that I've been thinking of doing would be to read the filesize and skip the large files which I will process separately, but I imagine there is a better way.keywords might be the phrase: these are my keywords I want to save a bunch of cha ay span across lines like so: these are my keywords. This is one reason I was us
|
---|