I just wanted to add my solution to get the inverse of
grep dog -B 1:
cat file | grep "$(cat file | grep dog -B 1)" -v
bird the opposite of what
cat file | grep dog -B 1 produces.
It seems my original problem was just misunderstanding how this should work. I was thinking applying
-v would simply inverse any
In actuality it seems the
-v flag was run first and then
-B 1 is applied after, so all lines not containing
dog are returned and then all lines 1 before those lines, meaning all lines.
Edit: I realized this solution is wrong. It doesn't inverse because the second grep using the sub-shell doesn't treat
"$(cat file | grep dog -B 1)" as a single text block. Instead each line generated is treated as a different grep.
If the subshell includes:
And the original file includes other instances of
cat that aren't 1 position before
dog they will be removed to with this solution.