grep語法格式
egrep 等價於 grep -E
管道方式使用grep 示例 $ cat /etc/passwd | grep "bash"
第一種形式:
grep [option] [pattern] [file1,file2...]
第二種形式
command | grep [option] [pattern]
grep 參數
grep 參數
選項 含義
-v 不顯示匹配行信息
-i 搜索時忽略大小寫
-n 顯示行號
-r 遞歸搜索
-E 支持拓展正則表達式
-F 不按正則表達式匹配,按照字符串字面意思匹配
-c 顯示匹配行總數
-w 匹配整詞
-x 匹配整行
-l 顯示文件名稱不顯示內容
-s 不顯示錯誤信息
示例:
$ grep python file
i love python
lovelove python
排除關鍵字-v
$ grep -v python file
Hello World
忽略大小寫 -i
$ grep -i h file
i love python
Hello World
lovelove python
顯示行號 -n
$ grep -n h file
1:i love python
3:lovelove python
擴展正則表達式 -E
$ grep -E "python|Hello" file
i love python
Hello World
lovelove python
按照字面意思查找 py.* 不按照正則表達式查找 -F
$ grep -F "py.*" file
$
全文搜索並且顯示行號
$ grep love -rn
aaa:1:i love python
aaa:3:lovelove python
file:1:i love python
file:3:lovelove python
匹配行數 -c
$ grep -c love file
2
匹配單詞 搜索條件必須前後有空格 -w
l$ grep -w love file
i love python
匹配整行信息 -x
grep -x "i love python" file
i love python
只顯示文件名字 -l
$ grep -l love file
file
在輸出命令用grep 命令
$ cat /etc/passwd | grep "bash"
root:x:0:0:root:/root:/bin/bash