linux基礎命令 grep
Linux系統中grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹 配的行打印出來。grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權限是所有用戶。
grep的工作方式是這樣的,它在一個或多個文件中搜索字符串模板。如果模板包括空格,則必須被引用,模板後的所有字符串被看作文件名。搜索的結果被送到標準輸出,不影響原文件內容。
grep可用於shell腳本,因為grep通過返回一個狀態值來說明搜索的狀態,如果模板搜索成功,則返回0,如果搜索不成功,則返回1,如果搜索的文件不存在,則返回2。我們利用這些返回值就可進行一些自動化的文本處理工作
linux三劍客老三,過濾需要的內容,-v排除內容 ,後面接要排除的內容
例子:grep -v oldboy test.txt
grep 20 -A 10 ett.txt 顯示包括20在內的後10行 20-30
-B除了顯示匹配的一行之外,並顯示該行之前的num行
-A除了顯示匹配的一行之外,並顯示該行之後的num行
-C除了顯示匹配的一行之外,並顯示該行之前後各num行
--color=auto對過濾的匹配的字符加顏色
-n表示對匹配的內容打印行號
2.命令功能:
用於過濾/搜索的特定字符。可使用正則表達式能多種命令配合使用,使用上十分靈活
3.命令參數:
-A<顯示行數> --after-context=<顯示行數> #除了顯示符合範本樣式的那一列之外,並顯示該行之後的內容
-B<顯示行數> --before-context=<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前的內容。
-C<顯示行數> --context=<顯示行數>或-<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前後內容
-i --ignore-case #忽略字符大小寫的差別。
-n --line-number #在顯示符合樣式的那一行之前,標示出該行的列數編號。
-v 排除內容 ,後面接要排除的內容,顯示不包含匹配文本的所有行。
4.grep的規則表達式:
^ #錨定行的開始 如:‘^grep‘匹配所有以grep開頭的行。
$ #錨定行的結束 如:‘grep$‘匹配所有以grep結尾的行。
. #匹配一個非換行符的字符 如:‘gr.p‘匹配gr後接一個任意字符,然後是p。
* #匹配零個或多個先前字符 如:‘*grep‘匹配所有一個或多個空格後緊跟grep的行。
.* #一起用代表任意字符。
[] #匹配一個指定範圍內的字符,如‘[Gg]rep‘匹配Grep和grep。
[^] #匹配一個不在指定範圍內的字符,如:‘[^A-FH-Z]rep‘匹配不包含A-R和T-Z的一個字母開頭,緊跟rep的行。
\(..\) #標記匹配字符,如‘\(love\)‘,love被標記為1。
\< #錨定單詞的開始,如:‘\<grep‘匹配包含以grep開頭的單詞的行。
\> #錨定單詞的結束,如‘grep\>‘匹配包含以grep結尾的單詞的行。
x\{m\} #重復字符x,m次,如:‘0\{5\}‘匹配包含5個o的行。
x\{m,\} #重復字符x,至少m次,如:‘o\{5,\}‘匹配至少有5個o的行。
x\{m,n\} #重復字符x,至少m次,不多於n次,如:‘o\{5,10\}‘匹配5--10個o的行。
\w #匹配文字和數字字符,也就是[A-Za-z0-9],如:‘G\w*p‘匹配以G後跟零個或多個文字或數字字符,然後是p。
\W #\w的反置形式,匹配一個或多個非單詞字符,如點號句號等。
\b #單詞鎖定符,如: ‘\bgrep\b‘只匹配grep。
5.使用實例:
實例一:查找指定進程
ps -ef|grep svn
[[email protected] data]# ps -ef|grep svn
root 2861 2656 0 04:45 pts/0 00:00:00 grep svn
實例二:查找指定進程個數 、
ps -ef|grep svn -c
ps -ef|grep -c svn
[[email protected] data]# ps -ef|grep -c svn
1
[[email protected] data]# ps -ef|grep svn -c
1
實例3:從文件中讀取關鍵詞進行搜索
命令:
cat test.txt | grep -f test2.txt
輸出:
[[email protected] test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[[email protected] test]# cat test2.txt
linux
Redhat
[[email protected] test]# cat test.txt | grep -f test2.txt
hnlinux
ubuntu linux
Redhat
linuxmint
[[email protected] test]#
說明:
輸出test.txt文件中含有從test2.txt文件中讀取出的關鍵詞的內容行
實例3:從文件中讀取關鍵詞進行搜索 且顯示行號
命令:
cat test.txt | grep -nf test2.txt
輸出:
[[email protected] test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[[email protected] test]# cat test2.txt
linux
Redhat
[[email protected] test]# cat test.txt | grep -nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[[email protected] test]#
說明:
輸出test.txt文件中含有從test2.txt文件中讀取出的關鍵詞的內容行,並顯示每一行的行號
實例五:從文件中查找關鍵詞
[[email protected] data]# cat b
i am a man
i am a woman
i am a girl
[[email protected] data]# grep ‘man‘ b
i am a man
i am a woman
實例六:從多個文件中查找關鍵詞
[[email protected] data]# cat a
you are not man
[[email protected] data]# cat b
i am a man
i am a woman
i am a girl
[[email protected] data]# grep ‘man‘ a b
a:you are not man
b:i am a man
b:i am a woman
實例七:找出以i開頭的行內容
[[email protected] data]# cat b | grep ‘^i‘
i am a man
i am a woman
i am a girl
實例八:找出以非i開頭的行內容
[[email protected] data]# cat b | grep ^[^i]
實例九:輸出以n結尾的行內容
[[email protected] data]# cat b|grep ‘n$‘
i am a man
i am a woman
實例十:顯示包含ed或者at字符的內容行
命令:
cat test.txt |grep -E "ed|at"
實例十一:顯示當前目錄下面以.txt 結尾的文件中的所有包含每個字符串至少有7個連續小寫字符的字符串的行
命令:
grep ‘[a-z]\{7\}‘ *.txt
linux基礎命令 grep