Using Raku (formerly known as Perl_6)
Return the first word matching "blue" per line and the 2 words that follow:
~$ raku -ne 'my @a = .words; my $i = @a.grep(/blue/, :k).first;
put splice(@a, $i, 3) with $i;' file
Raku is a Unicode-ready programming language in the Perl-family, thus perfectly situated for thorny text-processing problems. While this question can be solved using Regexes/Substrings*, it might be simpler (and more readable) to grep for the first word matching "blue", then splice-off and return the matching-word along with 2 more trailing words (3 words total), as above:
Sample Input:
this is a red cat in the room
this is a blue house at the street
this is a white mouse in the corner
this is a bluefin tuna in the bowl
Here are four: the blue tips of blue teeth of a blue mouse in a blue house
Sample Output (1):
blue house at
bluefin tuna in
blue tips of
Return every word matching "blue" per line, and the following 2 words for each:
~$ raku -ne 'my @a = .words; my @i = @a.grep(/blue/, :k).List;
for @i -> $i {my @b = @a; put splice(@b, $i, 3) with $i };' file
If you want to match every occurrence of "blue" in a line's worth of words (and not just the first occurrence), you can create a List of matches and iterate through them with the code above.
Sample Output (2):
blue house at
bluefin tuna in
blue tips of
blue teeth of
blue mouse in
blue house
Finally, it can get messy trying to keep track of the originating line with multiple matches per line. Below is code that numbers the input line:
~$ raku -ne 'BEGIN my $n; ++$n;
my @a = .words; my @i = @a.grep(/blue/, :k).List;
for @i -> $i {my @b = @a; put $n, ": ", splice(@b, $i, 3) with $i };' file
Sample Output (3):
2: blue house at
4: bluefin tuna in
5: blue tips of
5: blue teeth of
5: blue mouse in
5: blue house
*If you prefer up-to-ten characters following the word "blue" you can use this instead: raku -ne '$/.map( "blue" ~ * ).join("\n").put if m:g/ <?after blue> .**0..10 /;'
https://docs.raku.org/routine/splice
https://docs.raku.org
https://raku.org