【文本處理】grep 的用法

在這之前,補充一下正則表達式的知識

+       匹配1個或多個

*        匹配0個或多個

?        匹配0個或1個

{2,4}    匹配2~4個

{2,}        匹配至少2個

^        行首

$        行末

.        匹配任意一個字符

[^23]        匹配除2和3以外的任意字符

\        轉義



考慮到用戶有子母、數字、連接號和點號;域名由字母和數字組成

匹配郵箱地址的表達式[a-zA-Z0-9._]+@[a-zA-Z0-9.]+\.[a-zA-Z]{2,5}

匹配IP地址的表達式[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}


##### grep 用法 ######

匹配int 關鍵字

grep int  helloworld.c


帶顏色標紅匹配項

grep hello helloworld.c --color


匹配正則表達式

grep -E '[0-9]+'  test.txt

egrep '[0-9]+'  test.c


統計匹配項出現的次數

grep hello helloworld.c -o |wc -l


源代碼文件夾裏面遞歸匹配

cd src

grep  'myfunction()'  . -R -n 


忽略大小寫

Ryans$ grep -i 'heLlo' test.c

hello

heLLo 

HEllo


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