grep命令与正则表达式(过滤)二

前端之家收集整理的这篇文章主要介绍了grep命令与正则表达式(过滤)二前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

例子

1.过滤出带有某个关键词的行,并输出行号。

[root@gz1~]#grep-n'root'1.txt
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin

2.过滤出不带有某个关键词的,并输出行号。

[root@gz1~]#grep-vn'root'1.txt
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
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
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:uucp:x:10:14:uucp:/var/spool/uucp:/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:FTPUser:/var/ftp:/sbin/nologin
15:nobody:x:99:99:Nobody:/:/sbin/nologin
16:dbus:x:81:81:Systemmessagebus:/:/sbin/nologin
17:vcsa:x:69:69:virtualconsolememoryowner:/dev:/sbin/nologin
18:rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin
19:saslauth:x:499:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin
20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
21:haldaemon:x:68:68:HALdaemon:/:/sbin/nologin
22:rpcuser:x:29:29:RPCServiceUser:/var/lib/nfs:/sbin/nologin
23:nfsnobody:x:65534:65534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin
24:sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin
25:MysqL:x:500:500::/home/MysqL:/sbin/nologin

过滤出所有包含数字的行

[root@gz1~]#grep'[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
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:FTPUser:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:Systemmessagebus:/:/sbin/nologin
vcsa:x:69:69:virtualconsolememoryowner:/dev:/sbin/nologin
rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin
saslauth:x:499:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
haldaemon:x:68:68:HALdaemon:/:/sbin/nologin
rpcuser:x:29:29:RPCServiceUser:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin
MysqL:x:500:500::/home/MysqL:/sbin/nologin

过滤出所有包含a或N打字母行

[root@gz1~]#grep'[aN]'1.txt
root:x:0:0:root:/root:/bin/bash
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
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:FTPUser:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:Systemmessagebus:/:/sbin/nologin
vcsa:x:69:69:virtualconsolememoryowner:/dev:/sbin/nologin
rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin
saslauth:x:499:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
haldaemon:x:68:68:HALdaemon:/:/sbin/nologin
rpcuser:x:29:29:RPCServiceUser:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin

过滤出所有的大小写字母(正则表达式)

[root@gz1~]#grep'[a-zA-N]'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
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:FTPUser:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:Systemmessagebus:/:/sbin/nologin
vcsa:x:69:69:virtualconsolememoryowner:/dev:/sbin/nologin
rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin
saslauth:x:499:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
haldaemon:x:68:68:HALdaemon:/:/sbin/nologin
rpcuser:x:29:29:RPCServiceUser:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin
MysqL:x:500:500::/home/MysqL:/sbin/nologin

过滤出所有不包含大小字母(取反)

[root@gz1~]#grep-v'[a-zA-N]'1.txt
888888888888888888888888888888888888888888888888
&&&&&&&&&&&&&********************^^^^^^&^6

匹配以字母开头的行

[root@gz1~]#grep'^[a-z]'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
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:FTPUser:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:Systemmessagebus:/:/sbin/nologin
vcsa:x:69:69:virtualconsolememoryowner:/dev:/sbin/nologin
rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin
saslauth:x:499:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
haldaemon:x:68:68:HALdaemon:/:/sbin/nologin
rpcuser:x:29:29:RPCServiceUser:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin
MysqL:x:500:500::/home/MysqL:/sbin/nologin

匹配以数字开头的行

[root@gz1~]#grep'^[0-9]'1.txt
888888888888888888888888888888888888888888888888


匹配不包含数字的行

[root@gz1~]#grep'^[^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
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:FTPUser:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:Systemmessagebus:/:/sbin/nologin
vcsa:x:69:69:virtualconsolememoryowner:/dev:/sbin/nologin
rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin
saslauth:x:499:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
haldaemon:x:68:68:HALdaemon:/:/sbin/nologin
rpcuser:x:29:29:RPCServiceUser:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin
MysqL:x:500:500::/home/MysqL:/sbin/nologin
&&&&&&&&&&&&&********************^^^^^^&^6

匹配包含空行

[root@gz1~]#grep'^$'1.txt


匹配空行之外的行

[root@gz1~]#grep-v'^$'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
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:FTPUser:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:Systemmessagebus:/:/sbin/nologin
vcsa:x:69:69:virtualconsolememoryowner:/dev:/sbin/nologin
rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin
saslauth:x:499:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
haldaemon:x:68:68:HALdaemon:/:/sbin/nologin
rpcuser:x:29:29:RPCServiceUser:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin
MysqL:x:500:500::/home/MysqL:/sbin/nologin
888888888888888888888888888888888888888888888888
&&&&&&&&&&&&&********************^^^^^^&^6

特殊符号

.任意一个字符

[root@gz1~]#grep'r.o'1.txt
root:x:0:0:root:/root:/bin/bash
drooaemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:riox:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@gz1~]#cg'r.o'1.txt

*表示任意0或多个匹配

[root@gz1~]#cg'r*o'1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
drooaemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:riox:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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
ogames:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:Systemmessagebus:/:/sbin/nologin
vcsoa:x:69:69:virtualaconsolememoryowner:/dev:/sbin/nologin
rpc:x:32:32:RpcbindDa*emon:/var/cache/rpcbind:/sbin/nologin
sasloauth:x:499:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin
haldaemon:x:68:68:HALdaemon:/:/sbin/nologin
rpcuser:x:29:29:RPCrServiceUser:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65a534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin
MysqL:x:500:500::/home/MysqL:/sbin/nologin

任意任个字符 r开头o结尾(贪婪匹配)

[root@gz1~]#cg'r.*o'1.txt
root:x:0:0:root:/root:/bin/bash
drooaemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:riox:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:r_o7: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
ogames:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin
vcsoa:x:69:69:virtualaconsolememoryowner:/dev:/sbin/nologin
rpc:x:32:32:RpcbindDa*emon:/var/cache/rpcbind:/sbin/nologin
sasloauth:x:499:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin
rpcuser:x:29:29:RPCrServiceUser:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65a534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin

[root@gz1~]#cg'r\?o'1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
drooaemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:riox:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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
ogames:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:Systemmessagebus:/:/sbin/nologin
vcsoa:x:69:69:virtualaconsolememoryowner:/dev:/sbin/nologin
rpc:x:32:32:RpcbindDa*emon:/var/cache/rpcbind:/sbin/nologin
sasloauth:x:499:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin
haldaemon:x:68:68:HALdaemon:/:/sbin/nologin
rpcuser:x:29:29:RPCrServiceUser:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65a534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin
MysqL:x:500:500::/home/MysqL:/sbin/nologin
[root@gz1~]#cg-E'r?o'1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
drooaemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:riox:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:r_o7:lp:/var/spool/lpd:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
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
ogames:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:Systemmessagebus:/:/sbin/nologin
vcsoa:x:69:69:virtualaconsolememoryowner:/dev:/sbin/nologin
rpc:x:32:32:RpcbindDa*emon:/var/cache/rpcbind:/sbin/nologin
sasloauth:x:499:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/sp.oool/postfix:/sbin/nologin
haldaemon:x:68:68:HALdaemon:/:/sbin/nologin
rpcuser:x:29:29:RPCrServiceUser:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65a534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin
MysqL:x:500:500::/home/MysqL:/sbin/nologin
原文链接:https://www.f2er.com/regex/359402.html

猜你在找的正则表达式相关文章