Bash search command that checks preceeding characters

bash, grep, unix

Solution

Yes, a negative look-behind should help:

grep -P '(?<!stack)over'

Problem

Not sure if the title is worded correctly, but I am looking for a bash command that checks the characters before the search term. For example, if I have a file (lines.txt) that contains a list of words: ``` stackoverflow stack overflow stack over flow ``` I need the command that will search for the word `over`, but exclude any results that have `stack` immediately before it. so in the above example only the last two lines will hit. I have `grep -i 'over' lines.txt | grep -v 'stackover"`, but is there a better way/option of writing this? I've tinkered with a few other options (`-E` and `-P`) and have been reading up on negative lookaheads/behinds, but can't get the NOT part working.

Original source