這是 grep -P 中的錯誤嗎? (我似乎出現了太多匹配項)

這是 grep -P 中的錯誤嗎? (我似乎出現了太多匹配項)

這是我的一個文件 http://www.zen76171.zen.co.uk/blahsomefile1

這是一個大約1.18MB大小的文字文件

看看我得到了多少條匹配線

帶-P

C:\blah>grep -P "[^J]*J" blahsomefile1 | wc -l
72383

不帶-P

C:\blah>grep "[^J]*J" blahsomefile1 | wc -l
51814

無論是 -P 還是不帶 -P,都不應該有差別,但確實有差別。 -P 匹配太多。

通過這個測試,我應該得到相同的數字,因為我是說列出與 xyz 匹配的每一行,並在該輸出中列出與 xyz 匹配的每一行。無需 -P 即可運作。

沒有 -P 就沒有什麼有趣的事情發生。

C:\blah>grep "[^J]*J" blahsomefile1 | wc -l
51814

C:\blah>grep "[^J]*J" blahsomefile1 | grep "[^J]*J" | wc -l
51814

使用 -P 發生不應該發生的事情..

C:\blah>grep -P "[^J]*J" blahsomefile1 | wc -l
72383


C:\blah>grep -P "[^J]*J" blahsomefile1 | grep -P "[^J]*J" | wc -l
72229

如果我做grep -P "[^J]*J" blahsomefile1 | more

我發現它匹配了不應該匹配的內容,例如讀取的行txxxraabcAA 不包含 J。

txxxJbmmabcraabc
txxxraabcAA
txxxJxmmabcHaabc

電腦正在執行 gnuwin32 grep

C:\blah>where grep
C:\Program Files (x86)\GnuWin32\bin\grep.exe

grep 版本是 2.5.4

C:\blah>"C:\Program Files (x86)\GnuWin32\bin\grep.exe" -V
GNU grep 2.5.4

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.


C:\blah>

更新

有趣的是 cygwin 的版本要晚得多..並且沒有這個錯誤

C:\blah\aeea2\a\a\a\a>c:\cygwin\bin\grep -P "[^J]*J" blahsomefile1 | wc -l
51814

C:\blah>c:\cygwin\bin\grep -V
/usr/bin/grep (GNU grep) 2.21
Copyright (C) 2014 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.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.

C:\blah>

gnuwin32 grep 仍然在 2.5.4 上,這是一個非常舊的版本。

注意-文件也可用來自微轉帳並可下載來自 ge.tt例如火狐瀏覽器。

答案1

這可能是 grep 2.5.4 中的錯誤

如果您可以在 gnuwin32 grep 或 cygwin grep 之間進行選擇,則 cygwin grep 更新得多。

-V 顯示版本和年份,截至撰寫時 -

gnuwin32 grep 是2.5.42009 年的版本。

cygwin grep 比 gnuwin32 grep 領先幾年。 2.21(這比 2.5.4 晚,因為 2.21 不像一個帶有小數點的算術數字)並且 cygwin 的 grep 的年份是 2014 年。

C:\blah>c:\cygwin\bin\grep -P "[^J]*J" blahsomefile1 | wc -l
51814

C:\blah>c:\cygwin\bin\grep -P "[^J]*J" blahsomefile1 | c:\cygwin\bin\grep -P "[^J]*J" | wc -l
51814

使用 cygwin grep 沒有看到錯誤,即 2014 年。

這不是我第一次在 gnuwin32 版本的 grep 中遇到錯誤,當時 cygwin 版本的 grep 發布得晚得多,而且很好。與替代方案相比,gnuwin32 似乎非常過時。

相關內容