2013-07-31 37 views
1

有人可以請解釋/幫助我理解BSD grep和GNU grep之間的這種差異背後的邏輯嗎?我在比賽下面加了一些插文。BSD grep和GNU grep之間的詞邊界差異的邏輯

$ grep --version 
grep (BSD grep) 2.5.1-FreeBSD 
$ cat t1 
admin:*:80:root 
$ grep '\<.' t1 
admin:*:80:root 
^^^^^ ^^ ^^^^ 

$ grep --version 
GNU grep 2.6.3 

Copyright (C) 2009 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. 

$ cat t1 
admin:*:80:root 
$ grep '\<.' t1 
admin:*:80:root 
^  ^^ 

看來GNU grep的是得到它的權利和BSD grep的不是。我認爲\<應該得到字邊界?注grep '\<ad' t1似乎得到同樣的結果在這兩個版本,例如:

$ cat t2 
admin:*:80:root 
sadmin:*:81:root 
$ grep '\<ad' t2 
admin:*:80:root 
^^ 

是怎麼回事?

在此先感謝。

理查德

回答

1

使用\>指定字邊界不會爲BSD grep工作。改爲嘗試[[:<:]]

This post爲各種實用程序的字邊界提供了非常有用的信息。 「

+0

」長期以來,我的一個寵兒就是, 這個詞的正則表達式匹配對實現有着惱人的依賴。「 真實。 – Exit42