一 grep介绍
ps aux --sort=cpu ###查看进程排序cpu的
ps aux --sort=cpu |grep ssh
grep命令主要用于过滤文本,grep家族如下
grep: 在文件中全局查找指定的正则表达式,并打印所有包含该表达式的行
egrep:扩展的egrep,支持更多的正则表达式元字符
fgrep:固定grep(fixed grep),有时也被称作快速(fast grep),它按字面解释所有的字符
grep命令格式如下
grep [选项] PATTERN 文件1 文件2 ...
[root@egon ~]# grep 'root' /etc/passwd
[root@egon ~]# fgrep 'bash' /etc/passwd
找到: grep返回的退出状态为0
没找到: grep返回的退出状态为1
找不到指定文件: grep返回的退出状态为2
grep 命令的输入可以来自标准输入或管道,而不仅仅是文件,例如:
ps aux |grep 'nginx'
二 选项
-n, --line-number 在过滤出的每一行前面加上它在文件中的相对行号
-o, --only-matching 只显示匹配的内容
-q, --quiet, --silent 静默模式,没有任何输出,得用$?来判断执行成功没有,即有没有过滤到想要的内容
–color 颜色
-i, --ignore-case 忽略大小写
-A, --after-context=NUM 如果匹配成功,则将匹配行及其后n行一起打印出来
-B, --before-context=NUM 如果匹配成功,则将匹配行及其前n行一起打印出来
-C, --context=NUM 如果匹配成功,则将匹配行及其前后n行一起打印出来
-c, --count 如果匹配成功,则将匹配到的行数打印出来
-v, --invert-match 反向查找,只显示不匹配的行
-w 匹配单词
-E 等于egrep,扩展
-l, --files-with-matches 如果匹配成功,则只将文件名打印出来,失败则不打印
通常-rl一起用,grep -rl ‘root’ /etc
-R, -r, --recursive 递归
示例
###过滤以root开头的文件
[root@localhost ~]# grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
####-n显示行号
[root@localhost ~]# grep -n 'root' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
####-n显示行号以及以root开头
[root@localhost ~]# grep -n '^root' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
####-o只显示匹配的内容
[root@localhost ~]# grep -o 'root' /etc/passwd
root
root
root
root
[root@localhost ~]# alias grep ###与--color类似
alias grep='grep --color=auto'
###-A如果匹配成功,则将匹配行及其后n行一起打印出来
[root@localhost ~]# grep -A 2 '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
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
###-B如果匹配成功,则将匹配行及其前n行一起打印出来
[root@localhost ~]# grep -B 2 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
--
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
####-C 2如果匹配成功,则将匹配行及其前后n行一起打印出来
[root@localhost ~]# grep -C 2 '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
--
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
###-c如果匹配成功,则将匹配到的行数打印出来
[root@localhost ~]# grep -c 'root' /etc/passwd
2
####-v反向查找,只显示不匹配的行
[root@localhost ~]# grep -v 'root' /etc/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
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
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-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
libstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
yuan:x:1000:1000::/home/yuan:/bin/bash
####-w匹配单词
[root@localhost ~]# cat a.txt
egon123
egon 123
egon/123
eg*on
[root@localhost ~]# grep 'egon' a.txt
egon123
egon 123
egon/123
[root@localhost ~]# grep -w 'egon' a.txt
egon 123
egon/123
####-rl递归查询####如果配置文件忘记可以佩戴者-rl查询
[root@localhost ~]# grep -rl 'user' /usr/local/nginx/conf/
/usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf.default
####去掉自己带颜色的部分
[root@localhost ~]# ps aux |grep ssh
root 1462 0.0 0.4 110280 4172 ? Ss 10:06 0:00 /usr/sbin/sshd -D
root 4468 0.0 0.5 154160 5276 ? Ss 20:37 0:01 sshd: root@pts/0
root 5335 0.0 0.0 112828 980 pts/0 R+ 23:28 0:00 grep --color=auto ssh
[root@localhost ~]# ps aux |grep ssh |grep -v grep
root 1462 0.0 0.4 110280 4172 ? Ss 10:06 0:00 /usr/sbin/sshd -D
root 4468 0.0 0.5 154160 5276 ? Ss 20:37 0:01 sshd: root@pts/0
[root@localhost ~]# ps aux |grep [s]sh
root 1462 0.0 0.4 110280 4172 ? Ss 10:06 0:00 /usr/sbin/sshd -D
root 4468 0.0 0.5 154160 5276 ? Ss 20:37 0:01 sshd: root@pts/0
三 正则表达式
3.1 正则表达式介绍
元字符 功能 示例
^ 行首 ^love
$ 行尾 love$
. 除了换行符以外的任意单个字符 l..e
* 前导字符的零个或多个 ab*love
.* 所有字符 a.*love
[] 字符组内的任一字符 [lL]ove
[^] 对字符组内的每个字符取反(不匹配字符组内的每个字符) [^a-z0-9]ove
^[^] 非字符组内的字符开头的行
[a-z] 小写字母
[A-Z] 大写字母
[a-Z] 小写和大写字母
[0-9] 数字
\ 用来转义元字符 love\.
\< 词首定位符 单词一般以空格或特殊字符做分隔、连续的字符组成 \<love
\> 词尾定位符 love\>
\(..\) 匹配稍后将要使用的字符的标签 \(love\)able\1er
:1,$ s/\(192.168.11\).66/\1.50/g
x\{m\} 字符x重复出现m次 e\{3\}
x\{m,\} 字符x重复出现m次以上 e\{3,\}
x\{m,n\} 字符x重复出现m到n次 e\{3,6\}
示例
# 1、^ 行首
[root@egon ~]# grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@egon ~]#
# 2、$ 行尾
[root@egon ~]# grep 'bash$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
user1:x:1002:1003::/home/user1:/bin/bash
egon1:x:198:1005::/home/egon1:/bin/bash
gg:x:1004:1006::/home/gg:/bin/bash
egon:x:1005:1007::/home/egon:/bin/bash
tom:x:1006:1008::/home/tom:/bin/bash
###3. 除了换行符以外的任意单个字符
[root@egon ~]# grep 'r..t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
####4*代表前导字符的零个或多个
[root@localhost ~]# cat b.txt
a
ab
abb
abbbbbbbbba
abbbbabbbb
bbbbbbbbbbbbbbbbbbb
[root@localhost ~]# grep 'ab*' b.txt
a
ab
abb
abbbbbbbbba
abbbbabbbb
###.*代表贪婪
[root@localhost ~]# cat d.txt
ab
a1b
a-b
1111111111axxx$^31b123123b
b123123123
b12312ab231212
b123123b2132113a
[root@localhost ~]# grep 'a.*b' d.txt
ab
a1b
a-b
1111111111axxx$^31b123123b
b12312ab231212
# 5.1 .*?=》非贪婪,默认情况下,grep不支持非贪婪修饰符,但您可以使用grep -P来使用Perl语法来支持.*?
[root@localhost ~]# cat f.txt
<a href="http://www.baidu.com">"我他妈的是百度"</a>
<a href="http://www.sina.com.cn">"我特么的是新浪"</a>
[root@localhost ~]# grep 'href=".*"' f.txt
<a href="http://www.baidu.com">"我他妈的是百度"</a>
<a href="http://www.sina.com.cn">"我特么的是新浪"</a>
[root@localhost ~]# grep -P 'href=".*?"' f.txt
<a href="http://www.baidu.com">"我他妈的是百度"</a>
<a href="http://www.sina.com.cn">"我特么的是新浪"</a>
[root@localhost ~]# grep -oP 'href=".*?"' f.txt
href="http://www.baidu.com"
href="http://www.sina.com.cn"
[root@localhost ~]# grep -oP 'href=".*?"' f.txt |cut -d= -f2
"http://www.baidu.com"
"http://www.sina.com.cn"
##### 6、[] 字符组内的任一字符
[root@localhost ~]# cat d.txt
ab
a1b
a-b
a3b
a4b
a55544b
[root@localhost ~]# grep 'a[123]b*' d.txt
a1b
a3b
[root@localhost ~]# cat d.txt
ab
a1b
a-b
a3b
a4b
a55544b
axb
a*b
a-b
a/b
a+b
a34b
aABb
accb
[root@localhost ~]# grep 'a[123]b' d.txt
a1b
a3b
[root@localhost ~]# grep 'a[a-z]b' d.txt
axb
[root@localhost ~]# grep 'a[A-Z]b' d.txt
[root@localhost ~]# grep 'a[A-Z][A-Z]b' d.txt
aABb
[root@localhost ~]# grep 'a[0-9]b' d.txt
a1b
a3b
a4b
[root@localhost ~]# grep 'a[^0-9]b' d.txt
a-b
axb
a*b
a-b
a/b
a+b
[root@localhost ~]# grep 'a[-+*/]b' d.txt
a-b
a*b
a-b
a/b
a+b
[root@localhost ~]# grep 'a[^-+*/]b' d.txt
a1b
a3b
a4b
axb
# 7、[^] 对字符组内的每个字符取反(不匹配字符组内的每个字符)
[root@localhost ~]# grep '^a[^-+*/]b' d.txt
a1b
a3b
a4b
axb
[root@localhost ~]# grep '^a[-+*/]b' d.txt
a-b
a*b
a-b
a/b
a+b
[root@localhost ~]# grep 'a[0-9][0-9]' d.txt
a55544b
a34b
# 9、[a-z] 小写字母
# 10、[A-Z] 大写字母
# 11、[a-Z] 小写和大写字母
# 12、[0-9] 数字
# 13、\< 单词头 单词一般以空格或特殊字符做分隔,连续的字符串被当做单词
# 14、\> 单词尾
[root@localhost ~]# netstat -an |grep "22"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 36 10.0.0.100:22 10.0.0.1:50103 ESTABLISHED
tcp6 0 0 :::22 :::* LISTEN
unix 3 [ ] STREAM CONNECTED 22555 /run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 24722
unix 3 [ ] STREAM CONNECTED 22554
[root@localhost ~]# netstat -an |grep -w "22"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 36 10.0.0.100:22 10.0.0.1:50103 ESTABLISHED
tcp6 0 0 :::22 :::* LISTEN
[root@localhost ~]# netstat -an |grep "\<22\>"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 36 10.0.0.100:22 10.0.0.1:50103 ESTABLISHED
tcp6 0 0 :::22 :::* LISTEN
####+代表左边的字符出现1或者无穷次
Ps: grep匹配换行符和制表符
grep -Pz ‘eg\ton’ 7.txt TAB键
grep -Pz ‘eg\non’ 7.txt 换行
P代表正则 z代表多行处理
3.2.2 扩展正则元字符集
# 扩展正则元字符
+ 匹配一个或多个前导字符 [a-z]+ove
? 匹配零个或一个前导字符 lo?ve
a|b 匹配a或b love|hate
() 组字符 love(able|rs) (egon)+
(..)(..)\1\2 标签匹配字符 (love)able\1er
x{n} x出现n次 e{3}
x{n,} x出现n次至无穷次 e{3,}
x{n,m} x出现n次至m次 e{3,6}
# 若想使用扩展正则
grep加-E 或 egrep 或转义\
sed 加 -r 参数 或转义
AWK 直接支持大多数扩展正则,更多支持需要加选项--posix选项
示例
#####()的用法 egrep
[root@localhost ~]# cat g.txt
2221loverablelove32435
11111
2222egon
3321egonegonegonegon3211
egonegonegonegon2222
egon333322egon432
35234egonegongegonegonvcxz13332
[root@localhost ~]# grep '^[0-9]+(egon)+[0-9]+$' g.txt
[root@localhost ~]# egrep '^[0-9]+(egon)+[0-9]+$' g.txt
3321egonegonegonegon3211
####可以取分组的意思
[root@localhost ~]# cat g.txt
2221loveablelover32435
11111
2222egon
3321egonegonegonegon3211
egonegonegonegon2222
egon333322egon432
35234egonegongegonegonvcxz13332
[root@localhost ~]# egrep 'loveablelover' g.txt
2221loveablelover32435
[root@localhost ~]# egrep '(love)able\1r' g.txt
2221loveablelover32435
egrep ‘ab+’ 4.txt 代表从a开始b出现1次后者无穷次
egrep ‘ab*’ 4.txt 代表从a开始b出现0次后者无穷次
egrep ‘ab?’ 4.txt 代表从a开始b出现0次后者1次
{0,} ===== *
{1,} ===== +
{0,1}===== ?
a|b -----匹配a或b
示例
示例