Skip to main content
edited body
Source Link
cas
  • 83.9k
  • 8
  • 136
  • 205

Using GNU grep:

$ grep -oP '(\w+)?blue\w*'\w*blue\w*(\s*\w+)?' input.txt 
blue house
blue mouse

This shows the complete word containing the match and the following word (if any).

Note that with \w, underscores are considered to be "word" characters...so, for example, "light_blue" would be considered one word, while "light-blue" would not. If you want the regex to treat any non-space character as part of the word, use \S instead of \w. e.g.

$ grep -oP '(\S+)?blue\S*'\S*blue\S*(\s*\S+)?' input.txt 
blue house
blue mouse
light_blue mouse
light-blue mouse
blue-grey mouse

Using GNU grep:

$ grep -oP '(\w+)?blue\w*(\s*\w+)?' input.txt 
blue house
blue mouse

This shows the complete word containing the match and the following word (if any).

Note that with \w, underscores are considered to be "word" characters...so, for example, "light_blue" would be considered one word, while "light-blue" would not. If you want the regex to treat any non-space character as part of the word, use \S instead of \w. e.g.

$ grep -oP '(\S+)?blue\S*(\s*\S+)?' input.txt 
blue house
blue mouse
light_blue mouse
light-blue mouse
blue-grey mouse

Using GNU grep:

$ grep -oP '\w*blue\w*(\s*\w+)?' input.txt 
blue house
blue mouse

This shows the complete word containing the match and the following word (if any).

Note that with \w, underscores are considered to be "word" characters...so, for example, "light_blue" would be considered one word, while "light-blue" would not. If you want the regex to treat any non-space character as part of the word, use \S instead of \w. e.g.

$ grep -oP '\S*blue\S*(\s*\S+)?' input.txt 
blue house
blue mouse
light_blue mouse
light-blue mouse
blue-grey mouse
Source Link
cas
  • 83.9k
  • 8
  • 136
  • 205

Using GNU grep:

$ grep -oP '(\w+)?blue\w*(\s*\w+)?' input.txt 
blue house
blue mouse

This shows the complete word containing the match and the following word (if any).

Note that with \w, underscores are considered to be "word" characters...so, for example, "light_blue" would be considered one word, while "light-blue" would not. If you want the regex to treat any non-space character as part of the word, use \S instead of \w. e.g.

$ grep -oP '(\S+)?blue\S*(\s*\S+)?' input.txt 
blue house
blue mouse
light_blue mouse
light-blue mouse
blue-grey mouse