>>2522This is a surprisingly hard problem... I think it can be done with PCRE, which is supported by grep, but it's not nice.
First, match for all words:
> $ echo what wat lat latte blat | grep -P '\b\w+\b'> what wat lat latte blatI bolded the whole but the spaces are actually not highlighted!!\w means "word" character, so not whitespace, and \b means the beginning or end of a word. Next, try a negative lookahead. It looks like "(?!PATTERN)" and means that whatever comes next, cannot match PATTERN.
> $ echo what wat lat latte blat | grep -P '\b(?!lat)\w+\b'> what wat lat latte blatThis did not catch "latte", because it starts with "lat"... Maybe a negative lookbehind would work? It look like "(?<!PATTERN)" and means that anything you matched before, cannot match PATTERN.
> $ echo what wat lat latte blat | grep -P '\b\w+(?<!lat)\b'> what wat lat latte blatThis did not catch "blat", because it ends with "lat"...
My solution would be to say, if it is three letters long and those three letters are "lat", don't match. The only way I know to express is to have three cases: less than three letters, exactly three letters, more than three letters. I ended up with this:
> $ echo what wat lat latte blat la | grep -P '\b(\w{1,2}|(?!lat)\w{3}|\w{4,})\b'> what wat lat latte blat laWhich is ugly as hell but matches words expect for "lat". Which might not be what you actually wanted...
The problem is that if it's not matching words, "lat" will inevitably get included in two parts, like first matching "what wat l" and then "at latte blat la", or something. Maybe there's an easier way...
Why do you want to do this?