grep有時顯示會變成空行 - Linux

Table of Contents

我用鳥哥的文件做練習:
http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt

其中5,6,7,8,9,14這幾行是WINDOWS的結尾^M$,而我用的參數是
grep -n --color=auto ...,目的是看到行號和標示kwy word。

今天鳥哥示範『至少兩個 o 以上的字串』時,就需要 ooo*
grep -n --color=auto ooo* regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!

可以抓到1,2,3,9,18,19沒問題,但我想說*代表0到無限所以我用任意字元.也可以

grep -n --color=auto oo.* regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste gmood.
18:google is the best tools for search keyword.
19:goooooogle yes!

但這時候第9行就消失了,變成空行在那邊。只要不用.,隨便改用%或是H等字元,
都不會鬼隱。

另外如果我把結果存成文字,兩種選項的內容一樣。而如果我把--color=auto拿掉的話,
第9行就會出現。

我用過別種關鍵字,發現鬼隱的條件有三個:

1.抓的內容是.*結尾
2.斷行是^M$
3.要加上顏色顯示的參數

現在雖然沒什麼大問題,但我實在很好奇為什麼會有這種結果,請問有人知道嗎?

--
起初,他們追殺共產主義者,我沒有說話,因為我不是共產主義者;
接著,他們追殺猶太人,我沒有說話,因為我不是猶太人;
後來,他們追殺工會成員,我沒有說話,因為我不是工會成員;
此後,他們追殺天主教徒,我沒有說話,因為我是新教教徒;
最後,他們奔我而來,卻再也沒有人站起來為我說話了。
《First They Came(他們首次來時)》,Martin Niemoller牧師(1892-1984)

--

All Comments

Linda avatarLinda2011-06-11
grep -n --color=auto 'oo\.*' regular_express.txt
Rachel avatarRachel2011-06-12
你沒注意到他的顏色整個不一樣
Victoria avatarVictoria2011-06-17
我猜是 grep 的 --color=auto 輸出遇到 DOS 斷行時出問題
Rosalind avatarRosalind2011-06-17
您可以把輸入結果 > /tmp/1 然後試試看就明白了
Jake avatarJake2011-06-18
@dou0228:因為句尾不一定是句號,所以我用任意字元,不跳脫
Faithe avatarFaithe2011-06-18
@Tetralet:有試過,確實stdout到檔案的話就正常,不過顏色
就沒有了。
Joseph avatarJoseph2011-06-23
那就翻一下原始碼吧 我論一定是 bug... XD