作用:從文本文件或管道數(shù)據(jù)流中篩選匹配的行及數(shù)據(jù),配合正則表達(dá)式一起使用,功能更加強(qiáng)大。
格式:
grep [options] [pattern] [file]
1,匹配包含"ghostwu"的行
ghostwu@dev:~/linux/grep$%20cat%20-n%20ghostwu.txt%20 %20%20%20%20%201%20%20%20%20my%20name%20is%20ghostwu %20%20%20%20%202%20%20%20%20how%20are%20you %20%20%20%20%203%20%20%20%20fine%20think%20you %20%20%20%20%204%20%20%20%20My%20name%20is%20Ghostwu %20%20%20%20%205%20%20%20%20what's%20your%20name? %20%20%20%20%206%20%20%20%20my%20name%20is%20ghostwu2 %20%20%20%20%207%20%20%20%20 ghostwu@dev:~/linux/grep$%20grep%20"ghostwu"%20ghostwu.txt%20 my%20name%20is%20ghostwu my%20name%20is%20ghostwu2
2,-v:%20不包含,相當(dāng)于取反
ghostwu@dev:~/linux/grep$%20grep%20-v%20"ghostwu"%20ghostwu.txt%20 how%20are%20you fine%20think%20you My%20name%20is%20Ghostwu what's%20your%20name? ghostwu@dev:~/linux/grep$%20
3,-n添加行號(hào)
ghostwu@dev:~/linux/grep$ grep -n "ghostwu" ghostwu.txt 1:my name is ghostwu 6:my name is ghostwu2 ghostwu@dev:~/linux/grep$ grep -vn "ghostwu" ghostwu.txt 2:how are you 3:fine think you 4:My name is Ghostwu 5:what's your name? 7:
4linux常用命令,-E,使用擴(kuò)展的egrep命令,模式中可以用正則表達(dá)式
ghostwu@dev:~/linux/grep$ cat ghostwu.txt my name is ghostwu how are you fine think you My name is Ghostwu what's your name? my name is ghostwu2 ghostwu@dev:~/linux/grep$ grep -E "my|your" ghostwu.txt my name is ghostwu what's your name? my name is ghostwu2 ghostwu@dev:~/linux/grep$ grep -Ev "my|your" ghostwu.txt how are you fine think you My name is Ghostwu ghostwu@dev:~/linux/grep$ grep -En "my|your" ghostwu.txt 1:my name is ghostwu 5:what's your name? 6:my name is ghostwu2
5,-i選項(xiàng),不區(qū)分大小寫
ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt my name is ghostwu my name is ghostwu2 ghostwu@dev:~/linux/grep$ grep -i "ghostwu" ghostwu.txt my name is ghostwu My name is Ghostwu my name is ghostwu2
上一個(gè)教程:Linux操作系統(tǒng)之CentOS(一)
下一個(gè)教程:linux常見命令ps的應(yīng)用