searching/grepping for words "near" each other
    Kevin D. Clark 
    kevin_d_clark at comcast.net
       
    Thu Apr 30 12:02:19 EDT 2009
    
    
  
> I want to search a text file for a few (alphabetic) words which must
> be "near" each other, but not necessarily on the same line.  "Near"
> could be defined however you like... within a certain number of words
> from each other, a certain number of charecters from each other, or
> some similar constraint.
The following example looks for a certain famous phrase, but does so
in a "loose" manner, accepting anywhere from 1-200 lines of cruft
between the two parts of the phrase.  As far as the "cruft" goes, this
code doesn't care about linebreaks.
[pull-start the 500cc swiss-army chainsaw]
perl -0777 -lne 'print "$ARGV:$&" if (/weapons of.{1,200}mass destruction/s)' file1 file2 file3 ... fileN
("0777" causes Perl to "undef $/" (go into "slurp mode"), and the /s
regexp modifier causes "." to match newlines, which regexp engines
usually don't do)
--kevin
-- 
GnuPG ID: B280F24E                God, I loved that Pontiac.
alumni.unh.edu!kdc                -- Tom Waits
http://kdc-blog.blogspot.com/     
    
    
More information about the gnhlug-discuss
mailing list