Unix 快速入門/更多 grep 示例
外觀
可以使用符號 ^ 將搜尋限制為在行首查詢字串。示例
grep '^A' filename
在行首查詢字串 A。
可以使用符號 $ 將搜尋限制為在行尾查詢字串。示例
grep '5$' filename
在行尾查詢字串 5。
組合搜尋字串 ^$ 查詢空行。
元字元 . 匹配任何單個字元,除了行尾字元。
輸入檔案包含這些行
one
bone
throne
clone
我們用搜索
grep '.one' filename
結果是
bone
throne
clone
第一行不匹配。
元字元 * 匹配前一個字元的零個或多個出現次數。
輸入檔案 bells 包含這些行
bel
bell
belll
be
bet
我們用搜索
grep 'bel*' bells
結果是
bel
bell
belll
be
bet
輸入檔案與之前的示例相同。. 用於 * 之後,以要求至少有一個字元。
我們用搜索
grep 'bel*.' bells
結果是
bel
bell
belll
將其與之前的示例進行對比。在這裡,我們匹配除了 be 之外的所有內容。
輸入檔案與之前相同。
我們用搜索
grep 'bel.*' bells
結果是
bel
bell
belll
您可以使用用 [ 和 ] 括起來的字元列表,它將匹配列表中的任何單個字元。
輸入檔案是 lines
This is the zero line
Here y
Crosses x
我們用搜索
grep [xyz] lines
結果是
This is the zero line
Here y
Crosses x
輸入檔案與之前相同。
我們用搜索
grep [xyb] lines
結果是
Here y
Crosses x
