linux文件過濾
❶ linux命令grep
grep搜索成功,則返回0,如果搜索不成功,則返回1,如果搜索的文件不存在,則空祥雀返回2。
-n 列印行號
grep -n ".*" h.txt 所有列印行號
grep -n "root" h.txt 匹配的內容顯示行號
-v 不包括
-E 表示過濾 多個參數
grep -Ev "sshd|network|crond|sysstat|"
-o:僅列印你需要的東西,默認列印正行
grep -o "宴岩hello" h.txt
-i:忽略大小寫
grep -i "hello" h.txt
-c: 用於統計文中出現的次數
--color=auto 過濾欄位添加顏色
\b:作為邊界符,邊界只包含特定字元的行
grep "\boldboy\b" /etc/passwd -->只過濾包含oldboy的行
從多個文件中查找關鍵詞
grep "omc" /etc/passwd 斗早/etc/shadow
數據去重:cat log | sort | uniq
❷ Linux常用指令---grep(搜索過濾)(轉)
Linux常用指令---grep(搜索過濾) (轉)
Linux系統中grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹 配的行列印出來。grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用許可權是所有用戶。
grep的工作方式是這樣的,它在一個或多個文件中搜索字元串模板。如果模板包括空格,則必須被引用,模板後的所有字元串被看作文件名。搜索的結果被送到標准輸出,不影響原文件內容。
grep可用於shell腳本,因為grep通過返回一個狀態值來說明搜索的狀態,如果模板搜索成功,則返回0,如果搜索不成功,則返回1,如果搜索的文件不存在,則返回2。我們利用這些返回值就可進行一些自動化的文本處理工作。
1.命令格式:
grep [option] pattern file
2.命令功能:
用於過濾/搜索的特定字元。可使用正則表達式能多種命令配合使用,使用上十分靈活。
3.命令參數:
-a --text #不要忽略二進制的數據。
-A<顯示行數> --after-context=<顯示行數> #除了顯示符合範本樣式的那一列之外,並顯示該行之後的內容。
-b --byte-offset #在顯示符合樣式的那一行之前,標示出該行第一個字元的編號。
-B<顯示行數> --before-context=<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前的內容。
-c --count #計算符合樣式的列數。
-C<顯示行數> --context=<顯示行數>或-<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前後的內容。
-d <動作> --directories=<動作> #當指定要查找的是目錄而非文件時,必須使用這項參數,否則grep指令將回報信息並停止動作。
-e<範本樣式> --regexp=<範本樣式> #指定字元串做為查找文件內容的樣式。
-E --extended-regexp #將樣式為延伸的普通表示法來使用。
-f<規則文件> --file=<規則文件> #指定規則文件,其內容含有一個或多個規則樣式,讓grep查找符合規則條件的文件內容,格式為每行一個規則樣式。
-F --fixed-regexp #將樣式視為固定字元串的列表。
-G --basic-regexp #將樣式視為普通的表示法來使用。
-h --no-filename #在顯示符合樣式的那一行之前,不標示該行所屬的文件名稱。
-H --with-filename #在顯示符合樣式的那一行之前,表示該行所屬的文件名稱。
-i --ignore-case #忽略字元大小寫的差別。
-l --file-with-matches #列出文件內容符合指定的樣式的文件名稱。
-L --files-without-match #列出文件內容不符合指定的樣式的文件名稱。
-n --line-number #在顯示符合樣式的那一行之前,標示出該行的列數編號。
-q --quiet或--silent #不顯示任何信息。
-r --recursive #此參數的效果和指定「-d recurse」參數相同。
-s --no-messages #不顯示錯誤信息。
-v --revert-match #顯示不包含匹配文本的所有行。
-V --version #顯示版本信息。
-w --word-regexp #只顯示全字元合的列。
-x --line-regexp #只顯示全列符合的列。
-y #此參數的效果和指定「-i」參數相同。
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結尾的單詞的行。
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。
POSIX字元:
為了在不同國家的字元編碼中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字元類,如[:alnum:]是[A-Za-z0-9]的另一個寫法。要把它們放到[]號內才能成為正則表達式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支持POSIX的字元類。
[:alnum:] #文字數字字元
[:alpha:] #文字字元
[:digit:] #數字字元
[:graph:] #非空字元(非空格、控制字元)
[:lower:] #小寫字元
[:cntrl:] #控制字元
[:print:] #非空字元(包括空格)
[:punct:] #標點符號
[:space:] #所有空白字元(新行,空格,製表符)
[:upper:] #大寫字元
[:xdigit:] #十六進制數字(0-9,a-f,A-F)
5.使用實例:
實例1:查找指定進程
命令:
ps -ef|grep svn
輸出:
[root@localhost ~]# ps -ef|grep svn
root 4943 1 0 Dec05 ? 00:00:00 svnserve -d -r /opt/svndata/grape/
root 16867 16838 0 19:53 pts/0 00:00:00 grep svn
[root@localhost ~]#
說明:
第一條記錄是查找出的進程;第二條結果是grep進程本身,並非真正要找的進程。
實例2:查找指定進程個數
命令:
ps -ef|grep svn -c
ps -ef|grep -c svn
輸出:
[root@localhost ~]# ps -ef|grep svn -c
2
[root@localhost ~]# ps -ef|grep -c svn
2
[root@localhost ~]#
說明:
實例3:從文件中讀取關鍵詞進行搜索
命令:
cat test.txt | grep -f test2.txt
輸出:
[root@localhost test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -f test2.txt
hnlinux
ubuntu linux
Redhat
linuxmint
[root@localhost test]#
說明:
輸出test.txt文件中含有從test2.txt文件中讀取出的關鍵詞的內容行
實例3:從文件中讀取關鍵詞進行搜索 且顯示行號
命令:
cat test.txt | grep -nf test2.txt
輸出:
[root@localhost test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[root@localhost test]#
說明:
輸出test.txt文件中含有從test2.txt文件中讀取出的關鍵詞的內容行,並顯示每一行的行號
實例5:從文件中查找關鍵詞
命令:
grep 'linux' test.txt
輸出:
[root@localhost test]# grep 'linux' test.txt
hnlinux
ubuntu linux
linuxmint
[root@localhost test]# grep -n 'linux' test.txt
1:hnlinux
4:ubuntu linux
7:linuxmint
[root@localhost test]#
說明:
實例6:從多個文件中查找關鍵詞
命令:
grep 'linux' test.txt test2.txt
輸出:
[root@localhost test]# grep -n 'linux' test.txt test2.txt
test.txt:1:hnlinux
test.txt:4:ubuntu linux
test.txt:7:linuxmint
test2.txt:1:linux
[root@localhost test]# grep 'linux' test.txt test2.txt
test.txt:hnlinux
test.txt:ubuntu linux
test.txt:linuxmint
test2.txt:linux
[root@localhost test]#
說明:
多文件時,輸出查詢到的信息內容行時,會把文件的命名在行最前面輸出並且加上":"作為標示符
實例7:grep不顯示本身進程
命令:
ps aux|grep \[s]sh
ps aux | grep ssh | grep -v "grep"
輸出:
[root@localhost test]# ps aux|grep ssh
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
root 16901 0.0 0.0 61180 764 pts/0 S+ 20:31 0:00 grep ssh
[root@localhost test]# ps aux|grep \[s]sh]
[root@localhost test]# ps aux|grep \[s]sh
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
[root@localhost test]# ps aux | grep ssh | grep -v "grep"
root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshd
root 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0
說明:
實例8:找出已u開頭的行內容
命令:
cat test.txt |grep ^u
輸出:
[root@localhost test]# cat test.txt |grep ^u
ubuntu
ubuntu linux
[root@localhost test]#
說明:
實例9:輸出非u開頭的行內容
命令:
cat test.txt |grep ^[^u]
輸出:
[root@localhost test]# cat test.txt |grep ^[^u]
hnlinux
peida.cnblogs.com
redhat
Redhat
linuxmint
[root@localhost test]#
說明:
實例10:輸出以hat結尾的行內容
命令:
cat test.txt |grep hat$
輸出:
[root@localhost test]# cat test.txt |grep hat$
redhat
Redhat
[root@localhost test]#
說明:
實例11:輸出ip地址
命令:
ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"
輸出:
[root@localhost test]# ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]#
說明:
實例12:顯示包含ed或者at字元的內容行
命令:
cat test.txt |grep -E "ed|at"
輸出:
[root@localhost test]# cat test.txt |grep -E "peida|com"
peida.cnblogs.com
[root@localhost test]# cat test.txt |grep -E "ed|at"
redhat
Redhat
[root@localhost test]#
說明:
實例13:顯示當前目錄下面以.txt 結尾的文件中的所有包含每個字元串至少有7個連續小寫字元的字元串的行
命令:
grep '[a-z]\{7\}' *.txt
輸出:
[root@localhost test]# grep '[a-z]\{7\}' *.txt
test.txt:hnlinux
test.txt:peida.cnblogs.com
test.txt:linuxmint
[root@localhost test]#
實例14:日誌文件過大,不好查看,我們要從中查看自己想要的內容,或者得到同一類數據,比如說沒有404日誌信息的
命令:
grep '.' access1.log|grep -Ev '404' > access2.log
grep '.' access1.log|grep -Ev '(404|/photo/|/css/)' > access2.log
grep '.' access1.log|grep -E '404' > access2.log
輸出:
[root@localhost test]# grep 「.」access1.log|grep -Ev 「404」 > access2.log
說明:上面3句命令前面兩句是在當前目錄下對access1.log文件進行查找,找到那些不包含404的行,把它們放到access2.log中,後面去掉』v』,即是把有404的行放入access2.log
❸ Linux裡面grep -v命令作用是什麼
grep命令
grep
1.作用
Linux系統中grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹 配的行列印出來。grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用許可權是所有用戶。
grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的擴展,支持更多的re元字元, fgrep就是fixed grep或fast grep,它們把所有的字母都看作單詞,也就是說,正則表達式中的元字元表示回其自身的字面意義,不再特殊。linux使用GNU版本的grep。它功能更強,可以通過-G、-E、-F命令行選項來使用egrep和fgrep的功能。
2.格式及主要參數
grep [options]
主要參數: grep --help可查看
-c:只輸出匹配行的計數。
-i:不區分大小寫。
-h:查詢多文件時不顯示文件名。
-l:查詢多文件時只輸出包含匹配字元的文件名。
-n:顯示匹配行及 行號。
-s:不顯示不存在或無匹配文本的錯誤信息。
-v:顯示不包含匹配文本的所有行。
--color=auto :可以將找到的關鍵詞部分加上顏色的顯示。
pattern正則表達式主要參數:
\: 忽略正則表達式中特殊字元的原有含義。
^:匹配正則表達式的開始行。
$: 匹配正則表達式的結束行。
\<:從匹配正則表達 式的行開始。
\>:到匹配正則表達式的行結束。
[ ]:單個字元,如[A]即A符合要求 。
[ - ]:范圍,如[A-Z],即A、B、C一直到Z都符合要求 。
.:所有的單個字元。
*:所有字元,長度可以為0。
3.grep命令使用簡單實例
itcast$ grep 『test』 d*
顯示所有以d開頭的文件中包含 test的行
itcast $ grep 『test』 aa bb cc
顯示在aa,bb,cc文件中匹配test的行。
itcast $ grep 『[a-z]\{5\}』 aa
顯示所有包含每個字元串至少有5個連續小寫字元的字元串的行。
itcast $ grep 『wesest.*\1′ aa
如果west被匹配,則es就被存儲到內存中,並標記為1,然後搜索任意個字元(.*),這些字元後面緊跟著 另外一個es(\1),找到就顯示該行。如果用egrep或grep -E,就不用」\」號進行轉義,直接寫成』w(es)t.*\1′就可以了。
4.grep命令使用復雜實例
明確要求搜索子目錄:
grep -r
或忽略子目錄
grep -d skip
如果有很多輸出時,您可以通過管道將其轉到』less』上閱讀:
itcast$ grep magic /usr/src/Linux/Documentation/* | less
這樣,您就可以更方便地閱讀。
有一點要注意,您必需提供一個文件過濾方式(搜索全部文件的話用 *)。如果您忘了,』grep』會一直等著,直到該程序被中斷。如果您遇到了這樣的情況,按 ,然後再試。
下面還有一些有意思的命令行參數:
grep -i pattern files :不區分大小寫地搜索。默認情況區分大小寫,
grep -l pattern files :只列出匹配的文件名,
grep -L pattern files :列出不匹配的文件名,
grep -w pattern files :只匹配整個單詞,而不是字元串的一部分(如匹配』magic』,而不是』magical』),
grep -C number pattern files :匹配的上下文分別顯示[number]行,
grep pattern1 | pattern2 files :顯示匹配 pattern1 或 pattern2 的行,
例如:grep "abc\|xyz" testfile 表示過濾包含abc或xyz的行
grep pattern1 files | grep pattern2 :顯示既匹配 pattern1 又匹配 pattern2 的行。
grep -n pattern files 即可顯示行號信息
grep -c pattern files 即可查找總行數
還有些用於搜索的特殊符號:\< 和 \> 分別標注單詞的開始與結尾。
例如:
grep man * 會匹配 『Batman』、』manic』、』man』等,
grep 『\<man』 * 匹配』manic』和』man』,但不是』Batman』,
grep 『\<man\>』 只匹配』man』,而不是』Batman』或』manic』等其他的字元串。
『^』: 指匹配的字元串在行首,
❹ linux怎樣用grep過濾
你在配置文件里不可以用grep命令來過濾。grep只能在目錄文件上才能用。
❺ Linux如何過濾空文件
if test ! -s file 命令可以判斷是否是空文件,
把這段代碼添加到你的腳本裡面就可以過濾了。
❻ linux使用find命令如何過濾文件夾
先用find 查找的時候,制定不查找「.」開頭的文件夾
find /your/dir -type d ! -name ".*"