grep && 正則表達式

今天仔細研究了grep,

例如:文件bookword中

there is a book

there is a book.

there is a book'

there is a book)

there is a 'book

there is a 'book.

there is a (book.

there are some books.

there are some 'books.

there is a handbook.

book

books

handbook

如果只想匹配 book. ,

$ grep -n "[ ]book[.]" bookword

如果想匹配所有的book,

$ grep -n "/<book/>" bookword

 

這裏有幾個知識點

 

1. 精確匹配 /< 和 /> 可以去掉如"handbook" 或者"books"這樣的單詞

 

2. [“[{(]可以用在單詞前,用於匹配符號+單詞這樣的組合, 如: grep  "[(]book" bookword

there is a (book.

 

3. []})"?!.,;:'s], 可以用於單詞後,用於匹配符號+單詞,單詞複數 如 grep "book[.]" bookword

there is a book.

there is a 'book.

there is a (book.

there is a handbook.

 

4. (^|空格) 可以匹配單詞在行的開始或者一個空格.

5. (空格|$) 可以匹配單詞在行的結尾或者一個空格.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章