2012-03-22 37 views
2

我想从包含一些字符串的行清理我的日志文件。该字符串是在一个文件和由随机[键盘]字符的[肯定也*,+等] 好像grep的不*的精确匹配:fgrep匹配文字“*”

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来实现这一目标的标志?

谢谢!

回答

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 
+0

它也适用于我,fgrep 2.9 – 2012-03-22 19:16:20

3

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

让我们建立什么,我们正在处理:

[[email protected] ~]$ cat logs.txt 
ab 
blah_blah """abc*""" duh_duh 
abc 
[[email protected] ~]$ cat patterns.txt 
abc* 
[[email protected] ~]$ 

,并尝试grep和fgrep一样:

[[email protected] ~]$ grep -f patterns.txt logs.txt 
ab 
blah_blah """abc*""" duh_duh 
abc 
[[email protected] ~]$ fgrep -f patterns.txt logs.txt 
blah_blah """abc*""" duh_duh 
[[email protected] ~]$ 

正如你所看到的,图案是由grep解释为正则表达式,但作为字符串由fgrep

确认是否要匹配字符串模式,你就会知道,你应该使用grep的版本。