grep常見選項及命令


-E 按Extended規範進行匹配


--color 標記匹配顏色


-i 忽略大小寫匹配

[fbl@www test6_26]$ echo "hello" | grep -i --color 'HELLO'
hello


-q 安靜匹配 即不輸出內容

[fbl@www test6_26]$ echo "hello" | grep -q --color 'hello'
[fbl@www test6_26]$ echo $?
0

-o 只輸出匹配到的部分

[fbl@www test6_26]$ echo "hello" | grep -E -o --color 'hell*'
hell


-w 只顯示全字符合的lie

[fbl@www test6_26]$ cat file1
hello
helloo
hell
[fbl@www test6_26]$ cat file1 | grep -E --color 'hell'
hello
helloo
hell
[fbl@www test6_26]$ cat file1 | grep -wE --color 'hell'
hell


[fbl@www test6_26]$ cat file

apple

bahyude

nue

111123

88888

hello

change

brance

6666

[fbl@www test6_26]$ cat file | grep -E --color '^[a-z]{1,}[a-z]$'
apple
bahyude
nue
hello
change
brance

-c 輸出匹配到的列數

[fbl@www test6_26]$ cat file | grep -cE --color '^[a-z]{1,}[a-z]$'
6

-n 顯示匹配到的列及列號

[fbl@www test6_26]$ cat file | grep -nE --color '^[a-z]{1,}[a-z]$'
1:apple
2:bahyude
3:nue
6:hello
7:change
8:brance

-v 顯示不匹配的列

[fbl@www test6_26]$ cat file | grep -vE --color '^[a-z]{1,}[a-z]$'
111123
88888
6666

-a 偏移匹配

[fbl@www test6_26]$ cat file | grep -a2 --color 'hello'
111123
88888
hello
change
brance


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