The below code searches for the word 'located' in a text file. When it finds the word, it looks for 1 to 10 words after it, extracts the string, and writes the string to a file. The problem I am having is that the current script stops looking for up to the 10th word when it reaches a line break. I would like it to continue on to the next line up to the 10th word and then extract. As of right now I am getting results that look like this:
located around
located near the corner of West 86 and
located in the vicinity of
I would like to be able to get all ten words after 'located'. I am not sure if I'm using the '\n' operator correctly in the pattern or if that's even how I should do it. Any help is appreciated.
located around
located near the corner of West 86 and
located in the vicinity of
I would like to be able to get all ten words after 'located'. I am not sure if I'm using the '\n' operator correctly in the pattern or if that's even how I should do it. Any help is appreciated.
Code:
open(INFILE, "TheTextFile.txt");
open(OUTFILE, ">>locations.txt");
while(<INFILE>) {
chomp;
if (/located((?: \w+\n?|\W+\n?\w+\n?){1,10}\n?)+/g) {
$loc = $1\n\n";
print OUTFILE "located $loc";
}
}