简体   繁体   English

grep -f当模式文件也有一些空行时

[英]grep -f when the pattern file also has some empty lines

Is there any option of the grep command to suppress the effect of empty lines in the pattern file? grep命令是否可以抑制模式文件中空行的影响? I would prefer this option than always checking and filtering the nascent pattern file. 我宁愿选择此选项,而不是总是检查和过滤新生的模式文件。

Here is an example (in pattern_file , there is an empty line): 这是一个示例(在pattern_file ,有一个空行):

$ cat pattern_file  
APPLE  

PEAR  
$ cat file  
Nothing  
fruit  
$ grep -f pattern_file file  
Nothing  
fruit

要忽略病毒码文件中的空行:

grep -f <(grep . pattern_file) file
grep -v '^$'

This actually grabs all the lines except for newlines. 实际上,这会抓住换行以外的所有行。 You can pipe the result to your grep like this: grep -v '^$'| otherGrep 您可以像这样将结果通过管道传递到grep: grep -v '^$'| otherGrep grep -v '^$'| otherGrep

About -f option: 关于-f选项:

A null pattern can be specified by an empty line in pattern_file . 空模式可以由pattern_file中的空行指定。

So, it is a feature, and you should delete empty lines , eg with sed : 因此,这是一项功能,您应该删除空行 ,例如使用sed

sed '/^$/d' | grep -f ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM