2011-02-03 202 views
1

我有一個文本文件名爲 「文件1」 包含以下數據:UNIX grep命令

apple 
appLe 
app^e 
app\^e 

現在給出的命令是:

1.)grep app[\^lL]e file1 
2.)grep "app[\^lL]e" file1 
3.)grep "app[l\^L]e" file1 
4.)grep app[l\^L]e file1 

output in 1st case : app^e 

output in 2nd case : 
        apple 
        appLe 
        app^e 

output in 3rd case : 
        apple 
        appLe 
        app^e 

output in 4th case : 
        apple 
        appLe 
        app^e 

爲什麼這麼..?
請幫忙..!

+0

你的問題到底是什麼? – gnur 2011-02-03 17:09:31

+0

你期待輸出是什麼? – 2011-02-03 17:09:39

回答

5
1.)grep app[\^lL]e file1 

轉義(\)由殼的grep看到它,所以這是相當於app[^lL]e之前除去。括號中的比特匹配任何不在(從^,因爲它是第一個字符),L或l

2.)grep "app[\^lL]e" file1 

這一次,\脫^所以^或L或l

3.)grep "app[l\^L]e" file1 
匹配

^工程否定僅設置如果是第一個字符,所以這個匹配

4.)grep app[l\^L]e file1 

的^被轉義^或L或l,但因爲它是不是第一次它沒有任何區別,所以它匹配^或L或l

0

在第一個案例grep app[\^lL]e file1中,您不會在命令行中引用該模式,因此shell會處理其擴展。因此,搜索模式,有效,成爲

app[^lL]e 

和意思是: 「應用程序」,那麼任何碼元,但 「L」 或 「L」,然後 「E」。適合唯一的線是

app^e 

在其他情況下,^要麼逃走,字面匹配,或者,此外,它是在圖案的中間。