15

Say I have command1 which outputs this:

b05808aa-c6ad-4d30-a334-198ff5726f7c
59996d37-9008-4b3b-ab22-340955cb6019
2b41f358-ff6d-418c-a0d3-ac7151c03b78
7ac4995c-ff2c-4717-a2ac-e6870a5670f0

I also have command2 which outputs this:

b05808aa-c6ad-4d30-a334-198ff5726f7c
59996d37-9008-4b3b-ab22-340955cb6019

Is there a way to grep the output from command1 to not include any lines matched from command2, so that the final output would look like this?

2b41f358-ff6d-418c-a0d3-ac7151c03b78
7ac4995c-ff2c-4717-a2ac-e6870a5670f0
2
  • Probably doable with sort and awk Commented May 7, 2014 at 4:49
  • Are those lines? If so, yes. Commented May 7, 2014 at 4:50

2 Answers 2

29

Issue this grep

command1 | grep -vF -f <(command2)

Here,

-F means Fixed string match*

-v means invert match

-f means the file with patterns

<(command) actually creates a FIFO with that command and use it on redirection.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect thanks, I was missing the <() part, didn't know you could do that
6

To get all the lines from the output of command1 that do not appear in the output of command2:

grep -vFf <(command2) <(command1)

-f tells grep to use patterns that come from a file. In this case, that file is the output of command2. -F tells grep that those patterns are to be treated as fixed strings, not regex. -v tells grep to invert its normal behavior and just show lines the lines that do not match.

1 Comment

Thanks John, I upvoted you, but I marked the other answer just because I like the syntax better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.