如何过滤出文本文件中出现的命令输出行?


15

假设我们有一个禁止行文本文件forbidden.txt。过滤文本文件中存在的命令输出的所有行的简短方法是什么?

cat input.txt | exclude-forbidden-lines forbidden.txt | sort

4
fgrep -vxf forbidden.txt input.txt | sort

Answers:


26

grep像这样使用:

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

一长串的选择grep意味着

  • -v颠倒匹配感,即寻找匹配的事物。
  • -x匹配模式时,要求模式匹配整行,即不仅匹配上的任何地方。
  • -F匹配模式时,将其视为固定字符串,即不作为正则表达式。
  • -f从给定的文件(forbidden.txt)中读取模式。

然后将其通过管道传递给sort您或您想对其进行任何处理。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.