fgrep匹配文字“*”

时间:2012-03-22 18:59:52

标签: regex bash shell grep

我正在尝试从行中清除我的日志文件,而不是包含一些字符串。字符串在一个文件中,由随机[键盘]字符组成[是的*,+等] 似乎grep与*:

完全匹配
grep "abc*" logs :gives a matchine line
blah_blah """abc*""" duh_duh 

However when I read it off the file it doesnt work with fgrep:
cat file:
"abc*"
fgrep -f file logs => Matches nothing

我认为fgrep与grep + f [grep -f]相同。是否有一些我可以与fgrep一起使用的标志来实现这一点?

谢谢!

2 个答案:

答案 0 :(得分:3)

fgrep相当于grep -F,而不是grep -f-F选项匹配固定字符串,而不是模式。如果您尝试匹配文字字符串“abc *”,则与以“ab”开头并且后跟零个或多个“c”字符的正则表达式不同。

让我们确定我们正在合作的内容:

[ghoti@pc ~]$ cat logs.txt 
ab
blah_blah """abc*""" duh_duh
abc
[ghoti@pc ~]$ cat patterns.txt 
abc*
[ghoti@pc ~]$ 

尝试grep和fgrep:

[ghoti@pc ~]$ grep -f patterns.txt logs.txt 
ab
blah_blah """abc*""" duh_duh
abc
[ghoti@pc ~]$ fgrep -f patterns.txt logs.txt 
blah_blah """abc*""" duh_duh
[ghoti@pc ~]$ 

如您所见,该模式被grep解释为正则表达式,但被fgrep解释为文字字符串。

确认您是否匹配字符串模式,并且您将知道应该使用哪个版本的grep。

答案 1 :(得分:0)

您使用的是什么版本的grep?这可能是最近版本中修复的错误,因为一切对我来说都很好:

$ cat logs.txt
blah_blah """abc*""" duh_duh
$ cat patterns.txt
"abc*"
$ fgrep -f patterns.txt logs.txt
blah_blah """abc*""" duh_duh
$ fgrep --version
GNU grep 2.6.3