10月16日任務php
9.1 正則介紹_grep上shell
9.2 grep中編程
9.3 grep下vim
擴展centos
把一個目錄下,過濾全部*.php文檔中含有eval的行bash
grep -r --include="*.php" 'eval' /datassh
9.1 正則介紹_grep上編程語言
什麼是正則post
正則就是一串有規律的字符串學習
掌握好正則對於編寫shell腳本有很大幫助
各類編程語言中都有正則,原理是同樣的
本章將要學習grep/egrep、sed 、awk 三劍客
#Cent OS 7.0如下的版本grep和egrep是不帶顏色顯示的,須要手動添加alias。
[root@centos6 ~]# vim .bashrc [root@centos6 ~]# . .bashrc [root@centos6 ~]# which egrep alias egrep='egrep --color=auto' /bin/egrep [root@centos6 ~]# which grep alias grep='grep --color=auto' /bin/grep
grep
grep [-cinvABC] 'word' filename
-c 行數
-i 不區分大小寫
-n 顯示行號
-v 取反
-r 遍歷全部子目錄
-A 後面跟數字,過濾出符合要求的行以及下邊的n行
-B 同上,過濾出符合要求的行以及上邊的n行
-C 同上,同時過濾出符合要求的行以及上下各n行
[root@centos6 ~]# which grep
alias grep='grep --color=auto'
/bin/grep
[root@centos6 ~]# grep -c 'nologin' /etc/passwd
16
[root@centos6 ~]# grep -n 'nologin' /etc/passwd
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
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
15:nobody:x:99:99:Nobody:/:/sbin/nologin
16:vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
17:saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
18:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
20:ntp:x:38:38::/etc/ntp:/sbin/nologin
[root@centos6 ~]# grep -nv 'nologin' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
[root@centos6 ~]# grep -B2 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
--
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
[root@centos6 ~]# grep -A2 'root' /etc/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
--
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
[root@centos6 ~]# grep -C1 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/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
9.二、grep (中)
grep/egrep示例
grep -n 'root' /etc/passwd
grep -nv 'nologin' /etc/passwd
grep '[0-9]' /etc/inittab
grep -v '[0-9]' /etc/inittab
grep -v '^#' /etc/inittab
grep -v '^#' /etc/inittab -v '^$'
grep '^[^a-zA-Z]' text.txt # ^在方括號外表示以什麼開頭,在方括號內表示取非
grep 'r.o' test.txt # .表示一個字符
grep 'oo*' test.txt # *表示*左邊的字符重複0-n次
grep '.*' test.txt # .*表示匹配任意字符包括空行
grep 'o\{2\}' /etc/passwd # 特殊符號須要用脫意符號\,否則沒法識別
egrep 'o{2}' /etc/passwd # 用egrep命令,不須要脫意,{}表示前邊字符的重複範圍
egrep 'o+' /etc/passwd # +表示+左邊字符重複1-n次
egrep 'oo?' /etc/passwd # ?表示?前邊的字符重複0或者1次
egrep 'root|nologin' /etc/passwd # |表示或者
egrep '(oo){2}' /etc/passwd
[root@centos6 ~]# grep '[0-9]' 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 nobody:x:99:99:Nobody:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin [root@centos6 ~]# grep -v '[0-9]' passwd qwwwewer [root@centos6 ~]# vim inittab [root@centos6 ~]# grep -nv '#' inittab 26:aaaaaaaaaaaa 27:ashdfoaoiifuoa 28:111111111111111 30:id:3:initdefault: [root@centos6 ~]# grep -v '#' inittab aaaaaaaaaaaa ashdfoaoiifuoa 111111111111111 id:3:initdefault: [root@centos6 ~]# grep -n '#' inittab 1:# inittab is only used by upstart for the default runlevel. 2:# 3:# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. 4:# 5:# System initialization is started by /etc/init/rcS.conf 6:# 7:# Individual runlevels are started by /etc/init/rc.conf 8:# 9:# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf 10:# 11:# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, 12:# with configuration in /etc/sysconfig/init. 13:# 14:# For information on how to write upstart event handlers, or how 15:# upstart works, see init(5), init(8), and initctl(8). 16:# 17:# Default runlevel. The runlevels used are: 18:# 0 - halt (Do NOT set initdefault to this) 19:# 1 - Single user mode 20:# 2 - Multiuser, without NFS (The same as 3, if you do not have networking) 21:# 3 - Full multiuser mode 22:# 4 - unused 23:# 5 - X11 24:# 6 - reboot (Do NOT set initdefault to this) 25:# 29:(&*&^$^$^#%# [root@centos6 ~]# grep -n '^#' inittab 1:# inittab is only used by upstart for the default runlevel. 2:# 3:# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. 4:# 5:# System initialization is started by /etc/init/rcS.conf 6:# 7:# Individual runlevels are started by /etc/init/rc.conf 8:# 9:# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf 10:# 11:# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, 12:# with configuration in /etc/sysconfig/init. 13:# 14:# For information on how to write upstart event handlers, or how 15:# upstart works, see init(5), init(8), and initctl(8). 16:# 17:# Default runlevel. The runlevels used are: 18:# 0 - halt (Do NOT set initdefault to this) 19:# 1 - Single user mode 20:# 2 - Multiuser, without NFS (The same as 3, if you do not have networking) 21:# 3 - Full multiuser mode 22:# 4 - unused 23:# 5 - X11 24:# 6 - reboot (Do NOT set initdefault to this) 25:# [root@centos6 ~]# grep -n '^[0-9]' inittab 28:111111111111111 [root@centos6 ~]# grep -n '^[^0-9]' inittab 1:# inittab is only used by upstart for the default runlevel. 2:# 3:# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. 4:# 5:# System initialization is started by /etc/init/rcS.conf 6:# 7:# Individual runlevels are started by /etc/init/rc.conf 8:# 9:# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf 10:# 11:# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, 12:# with configuration in /etc/sysconfig/init. 13:# 14:# For information on how to write upstart event handlers, or how 15:# upstart works, see init(5), init(8), and initctl(8). 16:# 17:# Default runlevel. The runlevels used are: 18:# 0 - halt (Do NOT set initdefault to this) 19:# 1 - Single user mode 20:# 2 - Multiuser, without NFS (The same as 3, if you do not have networking) 21:# 3 - Full multiuser mode 22:# 4 - unused 23:# 5 - X11 24:# 6 - reboot (Do NOT set initdefault to this) 25:# 26:aaaaaaaaaaaa 27:ashdfoaoiifuoa 29:(&*&^$^$^#%# 30:id:3:initdefault:
9.三、grep(下)
[root@centos6 ~]# grep 'r.o' passwd root:x:0:0:root:/root:/bin/bash ahkdhfa:r>oo:dfsa:13243 operator:x:11:0:operator:/root:/sbin/nologin [root@centos6 ~]# grep 'ntp.*nologin' passwd #匹配以ntp開頭,nologin結束的行 ntp:x:38:38::/etc/ntp:/sbin/nologin [root@centos6 ~]# grep '{2}' passwd #錯誤示範 [root@centos6 ~]# grep 'o{2}' passwd #錯誤示範 [root@centos6 ~]# grep 'o\{2\}' passwd # {}須要加脫意符號\ root:x:0:0:root:/root:/bin/bash ahkdhfa:r>oo:dfsa:13243 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 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
#這樣操做起來特別麻煩,如何簡單操做呢,咱們能夠用到egrep命令了,或者grep -E
[root@centos6 ~]# egrep 'o{2}' passwd root:x:0:0:root:/root:/bin/bash ahkdhfa:r>oo:dfsa:13243 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 postfix:x:89:89::/var/spool/postfix:/sbin/nologin [root@centos6 ~]# grep -E 'o{2}' passwd root:x:0:0:root:/root:/bin/bash ahkdhfa:r>oo:dfsa:13243 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 postfix:x:89:89::/var/spool/postfix:/sbin/nologin [root@centos6 ~]# egrep 'o+t' passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@centos6 ~]# egrep 'o?t' 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 operator:x:11:0:operator:/root:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin [root@centos6 ~]# egrep 'o?1o' passwd bin:x:1o:1:bin:/bin:/sbin/nologin [root@centos6 ~]# grep -E 'root|nologin' passwd root:x:0:0:root:/root:/bin/bash bin:x:1o: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 nobody:x:99:99:Nobody:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin