http://www.perlmonks.org?node_id=62671

sub f{@a=();for(@_){push @_,shift;push @a,"@_"}@a}push@_,f split while + <>;print join "\n",sort @_
This program implements the Key Word In Context (KWIC) index system (in 98 bytes). This system accepts a sequence of lines of text and circularly shifts each line by repeatedly removing the first word and appending it to the end of the line. The KWIC system then outputs a listing of all circular shifts of all lines, in sorted order.

Run it with perl kwic.pl filename, where filename contains the lines you want to apply the KWIC system to.

This can probably be done more compactly... anybody out there got a shorter solution?