shell指令碼工具之grep命令

來源:互聯網
上載者:User

標籤:shell指令碼工具之grep命令

   grep(縮寫來自Globally search a Regular Expression and Print)是Linux系統的一種強大的文本搜尋工具,它能使用Regex搜尋文本,並把匹配的行列印出來.egrep和fgrep都是grep的擴充,支援更多的re元字元,fgrep就是fixed grep或fast grep.linux使用GNU版本的grep,它功能更強,可以通過-G、-E、-F命令列選項來使用egrep和fgrep的功能.grep可用於shell指令碼,因為grep通過返回一個狀態值來說明搜尋的狀態,如果模板搜尋成功,則返回0,如果搜尋不成功,則返回1,如果搜尋的檔案不存在,則返回2.我們利用這些傳回值就可進行一些自動化的文本處理工作.


grep的正規運算式元字元

^              行首定位器

$              行尾定位器

.              匹配任意一個字元

*              匹配0個或多個前置字元

[]             匹配指定範圍內的其中一個字元

[^]            匹配不要範圍內的字元

\<             詞首定位器

/〉            詞尾定位器

x\{m\}         重複x字元m次

x\{m,\}        重複x字元最少m次

x\{m,n\}       重複x字元m到n次


檔案內容:

[[email protected] opt]# ll passwd
-rw-r--r--. 1 root root 1087 Mar 19 17:39 passwd
[[email protected] opt]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[[email protected] opt]# 


1.grep命令格式

grep [選項] 字元模式 [檔案名稱1,檔案名稱2.........]


2.尋找r開頭的行

[[email protected] opt]# grep ‘^r‘ passwd
root:x:0:0:root:/root:/bin/bash
[[email protected] opt]#


3.尋找以c結尾的行

[[email protected] opt]# grep ‘c$‘ passwd
sync:x:5:0:sync:/sbin:/bin/sync
[[email protected] opt]#


4.尋找以h開頭,t結尾,中間只有兩個字元的行

[[email protected] opt]# grep ‘\<h..t\>‘ passwd
halt:x:7:0:halt:/sbin:/sbin/halt
[[email protected] opt]# 


5.尋找檔案內容只要有h或q的字元

[[email protected] opt]# grep ‘[hq]‘ passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
[[email protected] opt]# 


6.尋找每行有a到o字元出現7次的

[[email protected] opt]# grep ‘[a-o]\{7\}‘ passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[[email protected] opt]# 


7.在一些檔案中尋找相同的內容

[[email protected] opt]# grep root passwd*
passwd:root:x:0:0:root:/root:/bin/bash
passwd:operator:x:11:0:operator:/root:/sbin/nologin
passwd1:root:x:0:0:root:/root:/bin/bash
passwd1:operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] opt]#


8.顯示grep結果的行號

[[email protected] opt]# grep root -n passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] opt]#


9.顯示包含字元的檔案名稱

[[email protected] opt]# grep root -l passwd
passwd
[[email protected] opt]#


10.顯示檔案中的字元

[[email protected] opt]# grep root -c passwd
2
[[email protected] opt]# 


11.尋找內容是單詞

[[email protected] opt]# grep -w halt passwd
halt:x:7:0:halt:/sbin:/sbin/halt
[[email protected] opt]#


12.反向過濾

[[email protected] opt]# grep -v bash passwd | grep -v nologin passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
[[email protected] opt]#

13.用-E解釋萬用字元

[[email protected] opt]# grep -v -E ‘bash|nologin‘ passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
[[email protected] opt]#

本文出自 “一起走過的日子” 部落格,請務必保留此出處http://tongcheng.blog.51cto.com/6214144/1622319

shell指令碼工具之grep命令

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.