2012-03-22 29 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的版本。