14

Let's say we have a text file of forbidden lines forbidden.txt. What is a short way to filter all lines of a command output that exist in the text file?

cat input.txt | exclude-forbidden-lines forbidden.txt | sort
  • 4
    fgrep -vxf forbidden.txt input.txt | sort – Satō Katsura Jul 31 '16 at 18:35
24

Use grep like this:

$ grep -v -x -F -f forbidden.txt input.txt

That long list of options to grep means

  • -v Invert the sense of the match, i.e. look for things not matching.
  • -x When matching a pattern, require that the pattern matches the whole line, i.e. not just anywhere on the line.
  • -F When matching a pattern, treat it as a fixed string, i.e. not as a regular expression.
  • -f Read patterns from the given file (forbidden.txt).

Then pipe that to sort or whatever you want to do with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.