Linux 如何从文件中“grep”读取模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7027306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:39:15  来源:igfitidea点击:

How to make "grep" read patterns from a file?

regexlinuxshellgrep

提问by Michael

Suppose there is a large text file and I would like to print only the lines that do not match some patterns. Obviously, I can use egrep -v 'patter1|pattern2|pattern3. Now what if all those patterns are in a text file ? What is the best way to make egrepread patterns from the file ?

假设有一个大文本文件,我只想打印与某些模式不匹配的行。显然,我可以使用egrep -v 'patter1|pattern2|pattern3. 现在如果所有这些模式都在一个文本文件中呢?egrep从文件中读取模式的最佳方法是什么?

采纳答案by TMS

grep -v -f pattern_file

回答by Ernest Friedman-Hill

egrep has an -foption which does exactly that: you specify a file, and it reads patterns from that file, one per line.

egrep 有一个-f选项可以做到这一点:您指定一个文件,它从该文件中读取模式,每行一个。