grep
[root@abinlinux ~]# grep 'root' /etc/passwd 比對root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@abinlinux ~]# grep --color 'root' /etc/passwd --color 是顔色 用顔色來顯示
[root@abinlinux ~]# grep --color -n 'root' /etc/passwd 在那一行中出現 顯示在那一行 -n
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
[root@abinlinux ~]# alias ing='grep --color' 自定義别名
[root@abinlinux ~]# vim .bashrc 放到這個配置檔案中去
[1]+ Stopped vim .bashrc
[root@abinlinux ~]# fg
vim .bashrc
[root@abinlinux ~]# ing -c 'root' 1.txt -c出現了多少行 1.txt是/etc/passwd 複制過去的
2
[root@abinlinux ~]# grep --color -v 'root' 1.txt -v取反不包括root的行
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
[root@abinlinux ~]# ing -n -A 2 'root' 1.txt 把root 比對出來 還要多顯示兩行 -A
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
12-games:x:12:100:games:/usr/games:/sbin/nologin
13-gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
[root@abinlinux ~]# ing -n -B 2 'games' 1.txt 會往上多顯示兩行 -B
10-uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
11-operator:x:11:0:operator:/root:/sbin/nologin
12:games:x:12:100:games:/usr/games:/sbin/nologin
[root@abinlinux ~]# ing -n -C 2 'games' 1.txt 上下兩行都顯示
14-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@abinlinux ~]# ing -r 'iptables' /etc/* -r比對那個檔案
/etc/init.d/iptables:# iptables Start iptables firewall
[root@abinlinux ~]# ing -rh 'iptables' /etc/* 不想顯示前面的檔案 可以加個h
# iptables Start iptables firewall
# description: Starts, stops and saves iptables firewall
[root@binbinlinux ~]# ing -n 'yunjie' 1.txt 過濾出 yunjie并帶有 行号 -n 表示
25:yunjie:x:502:502::/home/yunjie:/bin/bash
[root@binbinlinux ~]# ing -v 'nologin' 1.txt 過濾出不帶有 nologin -v 表示
abin:x:500:500::/home/abin:/bin/bash
[root@binbinlinux ~]# ing '[0-9]' 1.txt 中括号 [0-9]表示不管是幾位 都能列出來
[root@binbinlinux ~]# ing '[cN]' 1.txt 取中括号[cN] 表示 列舉出來
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
[root@binbinlinux ~]# ing '[a-zA-Z]' 1.txt 正規表達式 [a-zA-Z] 列舉出來
nobody:x:99:99:Nobody:/:/sbin/nologin
user2:x:507:503::/home/user2:/bin/bash
user3:x:508:503::/home/user3:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
[root@binbinlinux ~]# ing -v '[a-zA-Z]' 1.txt -v取反 不包含這些的 就沒有
[root@binbinlinux ~]# vi 1.txt vim有個緩存檔案
[root@binbinlinux ~]# ls -l .1.txt.swp 檢視 swp
-rw-r--r-- 1 root root 16384 9月 8 17:57 .1.txt.swp
[root@binbinlinux ~]# rm -f .1.txt.swp 删除緩存檔案
vi 1.txt 加入純數字 純字元 和空行
[root@binbinlinux ~]# ing -v '[a-zA-Z]' 1.txt 可以把特殊字元行 和純數字行 和空行列舉出來
%#%$#%$#^%&^%&^**&^(&^(&^&(}{}{}}}}]]]][]][][][]
2222222222222222222222222222222222222222222222222222222
[root@binbinlinux ~]# ing '^[a-z]' 1.txt ^比對以字母開頭的行 1
[root@binbinlinux ~]# ing '^[0-9]' 1.txt ^比對 以數字開頭的行
[root@binbinlinux ~]# ing '[^0-9]' 1.txt ^放到中括号裡面就是非數字的行
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@binbinlinux ~]# ing '^[^0-9]' 1.txt ^放前面就是不以數字開頭的行
%#%$#%$#^%&^%&^**&^(&^(&^&(}{}{}}}}]]]][]][][][] 特殊字元也是比對到的
[root@binbinlinux ~]# ing '^$' 1.txt ^開頭 $結尾 這樣就把空行表示出來
[root@binbinlinux ~]# ing -v '^$' 1.txt 空行之外的表示出來
[root@binbinlinux ~]# ing 'r.o' 1.txt 比對到roo .表示任意的字元 空格也可以
[root@binbinlinux ~]# ing 'r.o' 1.txt 編輯 1.txt 加入字元 能比對
uucp:x:10:14:uucp:/var/spor_ol/uucp:/sbin/nologin
games:x:12:100:games:/usr/gamer*os:/sbin/nologin
vcsa:x:69:69:virtual cr.onsole memory ownoer:/dev:/sbin/nologin
[root@binbinlinux ~]# ing 'r*o' 1.txt 比對結果 oo比對兩次 rrr 要以o;結尾
shutdrwn:x:6:0:shutdown:/sbin:/sbin/shutdown
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/varrrrrrrrrrrrrrrrrrrrro/spor_ol/uucp:/sbin/nologin
operrrrrrrrrrrrrrrroator:x:11:0:operator:/root:/sbin/nologinii
* 0個或多個 *号前面的這個字元
[root@binbinlinux ~]# ing 'r.*o' 1.txt .是任意個字元 *是前面的字元或者是多個
root:x:0:0:root:/root:/bin/bash r開頭o結尾的列出來
bin:xrrrr:1:1:bin:/bin:/sbin/nologin
.* 叫做貪婪比對
[root@binbinlinux ~]# ing 'r?o' 1.txt 沒有結果
[root@binbinlinux ~]# ing 'r\?o' 1.txt 加上\表示 0或一個前面的字元
[root@binbinlinux ~]# ing -E 'r?o' 1.txt -E等于加\ 一個意思
bin:xrrrr:1:1:bin:/bin:/sbin/nloogin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin grep -E == egrep
[root@binbinlinux ~]# egrep --color 'r?o' 1.txt 和 grep --color ‘r\?o' 1.txt 一個意思
daemon:x:2:2:daemon:/sbin:/sbin/nologin ?号是個特殊字元要使用\符号或者egrep -E
[root@binbinlinux ~]# egrep --color 'r+o' 1.txt +号表示一個或多個前面的字元
[root@binbinlinux ~]# ing -E 'r+o' 1.txt grep -E 也可以
[root@binbinlinux ~]# ing 'r\+o' 1.txt 或者 加号\一下 去掉-E
operrrrrrrrrrrrrrrroator:x:11:0:operator:/root:/sbin/nologini
.任意一個字元(包括空格下劃線特殊符号) ; *零個或多個*前面的字元;.*任意個任意個字元 貪婪比對;?0或一個?前面的字元;+1個或多個+前面的字元
? + 使用grep -E grep ‘r\+o ’ 1.txt 要脫義 egrep 直接可以使用
[root@binbinlinux ~]# egrep --color 'root|nologin' 1.txt 并且或者
[root@binbinlinux ~]# grep --color 'root' 1.txt 比對一次
[root@binbinlinux ~]# grep --color 'root' 1.txt |grep --color 'nologin' 或者的表達方法
[root@abinlinux ~]# egrep --color 'root|nologin' 1.txt egrep 或者的使用方法
[root@abinlinux ~]# grep --color 'root' 1.txt grep要分兩次表達
[root@abinlinux ~]# grep --color 'root' 1.txt |grep --color 'nologin' 表達兩次
[root@binbinlinux ~]# egrep --color '(rr)+' 1.txt +号前面的多個成雙的r
[root@binbinlinux ~]# grep -E --color '(rr)+' 1.txt grep -E才可以使用
[root@binbinlinux ~]# grep -E --color '(rr){1,3}' 1.txt 比對一個範圍 1-3個
[root@binbinlinux ~]# grep -E --color '(rr){5}' 1.txt 範圍5
[root@binbinlinux ~]# grep -E --color '(rr){5,8}' 1.txt
[root@binbinlinux ~]# grep -E --color '(rr){10}' 1.txt
{} 指定它的次數的
[root@binbinlinux ~]# grep --color '\(rr\)\{10\}' 1.txt 使用grep 不用-E 使用\符号
+ ? () {} | 要使用egrep 或者grep -E 或者是 \符号
本文轉自 amenging 51CTO部落格,原文連結:http://blog.51cto.com/11335852/1983661