If your file contains paragraphs (ie. blank lines between reasonable sized block of text, something like this might work. It is pretty quick, processing a 587 MB test file below (replicated several dozen times to get a file of comparable size) in under a minute.

You can adjust the definition of a 'word' to suit. I've specified 1 to 5 words either side to account for the word being at the start or end of a paragraph.

#! perl -sw use strict; our $WORDS ||= 1; our $KEYWORD || die "-KEYWORD=word needed"; local $/ = ''; # Paragraph mode my $re_word = qr[\S+\s+]; my $re_5w_key_5w = qr[ ( $re_word {1,$WORDS} \Q($KEYWORD\E[,.;:!?]*\s+ $re_word {1,$WORDS} ) ]ix; open IN, '<', $ARGV[ 0 ] or die $!; while( <IN> ){ if( $_ =~ $KEYWORD ) { while( $_ =~ m[$re_5w_key_5w]g ) { print "'$1'\n---\n"; } } }

Results:

[ 0:58:39.10] P:\test>404751 -WORDS=1 -KEYWORD=poverty "Rhetoric - Ari +stotle.txt" 'or poverty; it ' --- 'or poverty but ' --- 'or poverty, of ' --- 'or poverty or ' --- 'his poverty, a ' --- 'in poverty or ' --- [ 0:58:50.09] P:\test>404751 -WORDS=3 -KEYWORD=poverty "Rhetoric - Ari +stotle.txt" 'to wealth or poverty; it is of ' --- 'to wealth or poverty but to appetite. ' --- 'of wealth or poverty, of being lucky ' --- 'by sickness or poverty or love or ' --- 'disregard of his poverty, a man aging ' --- 'by us in poverty or in banishment, ' --- [ 0:58:53.10] P:\test>404751 -WORDS=5 -KEYWORD=poverty "Rhetoric - Ari +stotle.txt" 'action due to wealth or poverty; it is of course true ' --- 'due not to wealth or poverty but to appetite. Similarly, with ' --- 'the sense of wealth or poverty, of being lucky or unlucky. ' --- 'are afflicted by sickness or poverty or love or thirst or ' --- 'man by disregard of his poverty, a man aging war by ' --- 'who stand by us in poverty or in banishment, even if ' ---

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

In reply to Re: searching for a keyword with context window by BrowserUk
in thread searching for a keyword with context window by fadingjava

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.