grep

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:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[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

1:root:x:0:0:root:/root:/bin/bash

2-bin:x:1:1:bin:/bin:/sbin/nologin

3-daemon:x:2:2:daemon:/sbin:/sbin/nologin

--

11:operator:x:11:0:operator:/root:/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     上下兩行都顯示

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

13-gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

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  表示  

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

abin:x:500:500::/home/abin:/bin/bash

[root@binbinlinux ~]# ing '[0-9]' 1.txt      中括號 [0-9]表示不管是幾位 都能列出來

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

[root@binbinlinux ~]# ing '[cN]' 1.txt        取中括號[cN] 表示  列舉出來  

sync:x:5:0:sync:/sbin:/bin/sync

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: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

[root@binbinlinux ~]# ing '^[0-9]' 1.txt              ^匹配 以數字開頭的行

2222222222222222222222222222222222222222222222222222222

[root@binbinlinux ~]# ing  '[^0-9]' 1.txt             ^放到中括號裏面就是非數字的行

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  

[root@binbinlinux ~]# ing '^[^0-9]' 1.txt             ^放前面就是不以數字開頭的行  

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

%#%$#%$#^%&^%&^**&^(&^(&^&(}{}{}}}}]]]][]][][][]          特殊字符也是匹配到的

[root@binbinlinux ~]# ing '^$' 1.txt                 ^開頭 $結尾  這樣就把空行表示出來

[root@binbinlinux ~]# ing -v '^$' 1.txt             空行之外的表示出來

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

[root@binbinlinux ~]# ing 'r.o' 1.txt                  匹配到roo   .表示任意的字符  空格也可以

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[root@binbinlinux ~]# ing 'r.o' 1.txt                編輯 1.txt 加入字符 能匹配

root:x:0:0:root:/root:/bin/bash

uucp:x:10:14:uucp:/var/spor_ol/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/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

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

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

.* 叫做貪婪匹配

[root@binbinlinux ~]# ing 'r?o' 1.txt            沒有結果

[root@binbinlinux ~]# ing 'r\?o' 1.txt          加上\表示 0或一個前面的字符    

root:x:0:0:root:/root:/bin/bash

bin:xrrrr: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

shutdrwn:x:6:0:shutdown:/sbin:/sbin/shutdown

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

[root@binbinlinux ~]# ing -E 'r?o' 1.txt          -E等於加\    一個意思

root:x:0:0:root:/root:/bin/bash

bin:xrrrr:1:1:bin:/bin:/sbin/nloogin

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            grep -E == egrep    


[root@binbinlinux ~]# egrep --color 'r?o' 1.txt     和 grep --color ‘r\?o'  1.txt   一個意思

root:x:0:0:root:/root:/bin/bash

bin:xrrrr:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin     ?號是個特殊字符要使用\符號或者egrep -E

[root@binbinlinux ~]# egrep --color 'r+o' 1.txt      +號表示一個或多個前面的字符

root:x:0:0:root:/root:/bin/bash

uucp:x:10:14:uucp:/varrrrrrrrrrrrrrrrrrrrro/spor_ol/uucp:/sbin/nologin

operrrrrrrrrrrrrrrroator:x:11:0:operator:/root:/sbin/nologinii

[root@binbinlinux ~]# ing -E 'r+o' 1.txt           grep -E 也可以

root:x:0:0:root:/root:/bin/bash

uucp:x:10:14:uucp:/varrrrrrrrrrrrrrrrrrrrro/spor_ol/uucp:/sbin/nologin

operrrrrrrrrrrrrrrroator:x:11:0:operator:/root:/sbin/nologinii

[root@binbinlinux ~]# ing 'r\+o' 1.txt      或者 加號\一下  去掉-E

root:x:0:0:root:/root:/bin/bash

uucp:x:10:14:uucp:/varrrrrrrrrrrrrrrrrrrrro/spor_ol/uucp:/sbin/nologin

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:x:0:0:root:/root:/bin/bash

bin:xrrrr: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

[root@binbinlinux ~]# grep --color 'root' 1.txt   匹配一次  

root:x:0:0:root:/root:/bin/bash

operrrrrrrrrrrrrrrroator:x:11:0:operator:/root:/sbin/nologinii

[root@binbinlinux ~]# grep --color 'root' 1.txt |grep --color 'nologin'  或者的表達方法  

operrrrrrrrrrrrrrrroator:x:11:0:operator:/root:/sbin/nologinii

[root@abinlinux ~]# egrep --color 'root|nologin' 1.txt       egrep 或者的使用方法  

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

[root@abinlinux ~]# grep --color 'root' 1.txt      grep要分兩次表達

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[root@abinlinux ~]# grep --color 'root' 1.txt |grep --color 'nologin'   表達兩次

operator:x:11:0:operator:/root:/sbin/nologin

[root@binbinlinux ~]# egrep --color '(rr)+' 1.txt      +號前面的多個成雙的r

bin:xrrrr:1:1:bin:/bin:/sbin/nologin

uucp:x:10:14:uucp:/varrrrrrrrrrrrrrrrrrrrro/spor_ol/uucp:/sbin/nologin

operrrrrrrrrrrrrrrroator:x:11:0:operator:/root:/sbin/nologinii

[root@binbinlinux ~]# grep -E  --color '(rr)+' 1.txt        grep -E纔可以使用  

bin:xrrrr:1:1:bin:/bin:/sbin/nologin

uucp:x:10:14:uucp:/varrrrrrrrrrrrrrrrrrrrro/spor_ol/uucp:/sbin/nologin

operrrrrrrrrrrrrrrroator:x:11:0:operator:/root:/sbin/nologinii

[root@binbinlinux ~]# grep -E  --color '(rr){1,3}' 1.txt      匹配一個範圍  1-3個  

bin:xrrrr:1:1:bin:/bin:/sbin/nologin

uucp:x:10:14:uucp:/varrrrrrrrrrrrrrrrrrrrro/spor_ol/uucp:/sbin/nologin

operrrrrrrrrrrrrrrroator:x:11:0:operator:/root:/sbin/nologinii

[root@binbinlinux ~]# grep -E  --color '(rr){5}' 1.txt          範圍5

uucp:x:10:14:uucp:/varrrrrrrrrrrrrrrrrrrrro/spor_ol/uucp:/sbin/nologin

operrrrrrrrrrrrrrrroator:x:11:0:operator:/root:/sbin/nologinii

[root@binbinlinux ~]# grep -E  --color '(rr){5,8}' 1.txt        

uucp:x:10:14:uucp:/varrrrrrrrrrrrrrrrrrrrro/spor_ol/uucp:/sbin/nologin

operrrrrrrrrrrrrrrroator:x:11:0:operator:/root:/sbin/nologinii

[root@binbinlinux ~]# grep -E  --color '(rr){10}' 1.txt

uucp:x:10:14:uucp:/varrrrrrrrrrrrrrrrrrrrro/spor_ol/uucp:/sbin/nologin

{}  指定它的次數的    

[root@binbinlinux ~]# grep --color '\(rr\)\{10\}' 1.txt     使用grep 不用-E 使用\符號  

uucp:x:10:14:uucp:/varrrrrrrrrrrrrrrrrrrrro/spor_ol/uucp:/sbin/nologin

+ ? () {}   |      要使用egrep     或者grep -E    或者是 \符號  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章