Skip to main content
added 253 characters in body
Source Link
Philip Kirkbride
  • 10.8k
  • 33
  • 107
  • 176

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 

Which returns 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 grep command.

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:

cat dog 

And the original file includes other instances of cat that aren't 1 position before dog they will be removed to with this solution.

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 

Which returns 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 grep command.

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.

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 

Which returns 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 grep command.

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:

cat dog 

And the original file includes other instances of cat that aren't 1 position before dog they will be removed to with this solution.

added 125 characters in body
Source Link
Philip Kirkbride
  • 10.8k
  • 33
  • 107
  • 176

I just wanted to add my solution to get the result I actually wantedinverse of grep dog -B 1:

cat file | grep "$(cat file | grep dog -B 1)" -v 

It seems my original problem was just misunderstanding how this should work.

I was thinking applyingWhich returns -vbird would simply inverse anythe opposite of what cat file | grep dog -B 1 commandproduces.

It seems my original problem was just misunderstanding how this should work. I was thinking applying -v would simply inverse any grep command.

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.

I just wanted to add my solution to get the result I actually wanted:

cat file | grep "$(cat file | grep dog -B 1)" -v 

It seems my original problem was just misunderstanding how this should work.

I was thinking applying -v would simply inverse any grep command.

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 

Which returns 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 grep command.

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.

Source Link
Philip Kirkbride
  • 10.8k
  • 33
  • 107
  • 176

I just wanted to add my solution to get the result I actually wanted:

cat file | grep "$(cat file | grep dog -B 1)" -v 

It seems my original problem was just misunderstanding how this should work.

I was thinking applying -v would simply inverse any grep command.