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
-vInvert the sense of the match, i.e. look for lines not matching.-xWhen matching a pattern, require that the pattern matches the whole line, i.e. not just anywhere on the line.-FWhen matching a pattern, treat it as a fixed string, i.e. not as a regular expression.-fRead 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