How to filter out lines of a command output that occur in a text file?

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 lines 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x