标签:grep
正则介绍[root@localhost ~]# mkdir grep
[root@localhost ~]# cd grep/
[root@localhost grep]# cp /etc/passwd .
[root@localhost grep]# ls
passwd
[root@localhost grep]# pwd
/root/grep
[root@localhost grep]# ls
passwd
[root@localhost grep]# grep ‘nologin‘ 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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
[root@localhost grep]#
[root@localhost grep]# grep -c ‘nologin‘ passwd
17
[root@localhost grep]#
[root@localhost grep]# grep -n ‘nologin‘ 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:operator:x:11:0:operator:/root:/sbin/nologin
11:games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13:nobody:x:99:99:Nobody:/:/sbin/nologin
14:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
15:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
16:dbus:x:81:81:System message bus:/:/sbin/nologin
17:polkitd:x:998:996:User for polkitd:/:/sbin/nologin
18:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
19:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
20:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
21:chrony:x:997:995::/var/lib/chrony:/sbin/nologin
[root@localhost grep]#
[root@localhost grep]# 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
user1:x:1000:1007::/home/user1:/bin/bash
user2:x:1001:1008::/home/user2:/bin/bash
user3:x:1005:1000::/home/user3:/bin/bash
[root@localhost grep]#
[root@localhost grep]# grep -r ‘root‘ /etc/
/etc/pki/ca-trust/ca-legacy.conf:# The upstream Mozilla.org project tests all changes to the root CA
/etc/pki/ca-trust/ca-legacy.conf:# to temporarily keep certain (legacy) root CA certificates trusted,
/etc/pki/ca-trust/ca-legacy.conf:# It may keep root CA certificate as trusted, which the upstream
/etc/pki/ca-trust/extracted/README:root CA certificates.
/etc/pki/ca-trust/extracted/java/README:root CA certificates.
匹配到二进制文件 /etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/README:root CA certificates.
/etc/pki/ca-trust/extracted/pem/README:root CA certificates.
/etc/pki/tls/certs/make-dummy-cert: echo root@localhost.localdomain
/etc/pki/tls/openssl.cnf:dir = ./demoCA # TSA root directory
/etc/rpm/macros.perl:%define perl_br_testdir %{buildroot}%{perl_testdir}/%{cpan_dist_name}
[root@localhost grep]# grep -nA2 ‘root‘ passwd
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
--
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@localhost grep]#
[root@localhost grep]# grep -nB2 ‘root‘ passwd
1:root:x:0:0:root:/root:/bin/bash
--
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost grep]#
[root@localhost grep]# grep -nC2 ‘root‘ passwd
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
--
8-halt:x:7:0:halt:/sbin:/sbin/halt
9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ft:x:14:50:FTP User:/var/ftp:/sbin/nologin
过滤出所有包含数字的行
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
user1:x:1000:1007::/home/user1:/bin/bash
user2:x:1001:1008::/home/user2:/bin/bash
user3:x:1005:1000::/home/user3:/bin/bash
如果加了-v ,意思是过滤出除了带数字的行,如下结果是没有这样的行
[root@localhost ~]# grep -v ‘[0-9]‘ /etc/passwd
[root@localhost ~]#
[root@localhost ~]# grep -n ‘^#‘ /etc/inittab
1:# inittab is no longer used when using systemd.
2:#
3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
4:#
5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
6:#
7:# systemd uses ‘targets‘ instead of runlevels. By default, there are two main targets:
8:#
9:# multi-user.target: analogous to runlevel 3
10:# graphical.target: analogous to runlevel 5
11:#
12:# To view current default target, run:
13:# systemctl get-default
14:#
15:# To set a default target, run:
16:# systemctl set-default TARGET.target
17:#
[root@localhost ~]# grep -nv ‘^#‘ /etc/inittab
[root@localhost ~]#
[]里面的内容是非的意思,上面的就是过滤列出一个非0-9开头的行。
r.o的.的意思是代表所有字符。
[root@localhost grep]# vi passwd
[root@localhost grep]# grep -n ‘r.o‘ passwd
1:root:x:0:0:root:/root:/bin/bash
3:fsfh:23:rao:fsdfafsaf:fffas
11:fdsafa:r>o:ksldjflkasdjfla:qe112312
12:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost grep]#
‘oo‘ 号左侧的字符的n个相同字符。
[root@localhost grep]# grep -n ‘o*o‘ passwd
1:root:x:0:0:root:/root:/bin/bash
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:fsfh:23:rao:fsdfafsaf:fffas
4:daemon:x:2:2:daemon:/sbin:/sbin/nologin
5:adm:x:3:4:adm:/var/adm:/sbin/nologin
6:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
8:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
10:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11:fdsafa:r>o:ksldjflkasdjfla:qe112312
12:operator:x:11:0:operator:/root:/sbin/nologin
13:games:x:12:100:games:/usr/games:/sbin/nologin
14:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
15:nobody:x:99:99:Nobody:/:/sbin/nologin
16:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
17:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
18:dbus:x:81:81:System message bus:/:/sbin/nologin
19:polkitd:x:998:996:User for polkitd:/:/sbin/nologin
20:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
21:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
22:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
23:chrony:x:997:995::/var/lib/chrony:/sbin/nologin
24:user1:x:1000:1007::/hooome/user1:/bin/bash
25:user2:x:1001:1008::/hoooome/user2:/bin/bash
26:user3:x:1005:1000::/hoome/user3:/bin/bash
[root@localhost grep]#
[root@localhost grep]# grep ‘o{2}‘ passwd
[root@localhost grep]# grep ‘o\{2\}‘ passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
user1:x:1000:1007::/hooome/user1:/bin/bash
user2:x:1001:1008::/hoooome/user2:/bin/bash
user3:x:1005:1000::/hoome/user3:/bin/bash
[root@localhost grep]#
[root@localhost grep]# egrep ‘o{2}‘ passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
user1:x:1000:1007::/hooome/user1:/bin/bash
user2:x:1001:1008::/hoooome/user2:/bin/bash
user3:x:1005:1000::/hoome/user3:/bin/bash
[root@localhost grep]# grep -E ‘o{2}‘ passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
user1:x:1000:1007::/hooome/user1:/bin/bash
user2:x:1001:1008::/hoooome/user2:/bin/bash
user3:x:1005:1000::/hoome/user3:/bin/bash
与 意思差不多, + 也是过滤 + 号前面字符的n次。但是次数时大于等于1的,必须出现一次或以及以上,下面的o至少是两个在一起的。和 不同, * 可以出现0次。
[root@localhost grep]# grep -n ‘o+o‘ passwd
[root@localhost grep]# grep -n ‘o\+o‘ passwd
1:root:x:0:0:root:/root:/bin/bash
6:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
10:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
12:operator:x:11:0:operator:/root:/sbin/nologin
21:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
24:user1:x:1000:1007::/hooome/user1:/bin/bash
25:user2:x:1001:1008::/hoooome/user2:/bin/bash
26:user3:x:1005:1000::/hoome/user3:/bin/bash
[root@localhost grep]# egrep -n ‘o+o‘ passwd
1:root:x:0:0:root:/root:/bin/bash
6:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
10:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
12:operator:x:11:0:operator:/root:/sbin/nologin
21:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
24:user1:x:1000:1007::/hooome/user1:/bin/bash
25:user2:x:1001:1008::/hoooome/user2:/bin/bash
26:user3:x:1005:1000::/hoome/user3:/bin/bash
[root@localhost grep]#
[root@localhost grep]# egrep -n ‘o+t‘ passwd
1:root:x:0:0:root:/root:/bin/bash
12:operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost grep]# egrep -n ‘o+b‘ passwd
15:nobody:x:99:99:Nobody:/:/sbin/nologin
[root@localhost grep]#
? 的意思是?号左侧字符出现0或者1次,要么有要么没有。在linux系统中会有红色高亮显示
[root@localhost grep]# egrep -n ‘o?t‘ passwd
1:root:x:0:0:root:/root:/bin/bash
8:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9:halt:x:7:0:halt:/sbin:/sbin/halt
12:operator:x:11:0:operator:/root:/sbin/nologin
14:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
16:systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
17:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
18:dbus:x:81:81:System message bus:/:/sbin/nologin
19:polkitd:x:998:996:User for polkitd:/:/sbin/nologin
20:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
21:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
22:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@localhost grep]#
| 或者的意思
[root@localhost grep]# egrep ‘root|nologin‘ 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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
[root@localhost grep]#
把一个目录下,过滤所有*.php文档中含有eval的行
grep -r --include="*.php" ‘eval‘ /data/
标签:grep
原文地址:http://blog.51cto.com/11751505/2107876