grep
主要参数:
-v 取反
-i 忽略大小写
^# 以井号键开头
^$ 空行
-n 对过滤的内容加上行号
实战演练:
[root@localhost ~]# grep "nologin|root" /etc/passwd|wc -l
0
[root@localhost ~]# grep "nologin\|root" /etc/passwd|wc -l #\转义字符生效
37
[root@localhost ~]# egrep "nologin|root" /etc/passwd |wc -l
37
[root@localhost ~]#
管道
管道命令是从左到右顺序执行前边的执行报错不再执行后续内容
演示:
[root@localhost ~]# ps -aux |grep "sshd" #过滤sshd相关进程
root 1097 0.0 0.0 105996 220 ? Ss 08:19 0:00 /usr/sbin/sshd -D
root 3381 0.0 0.0 112680 992 pts/0 S+ 08:41 0:00 grep --color=auto sshd
[root@localhost ~]# ps -aux |grep "sshd"|cat -n #过滤后加上序号
1 root 1097 0.0 0.0 105996 220 ? Ss 08:19 0:00 /usr/sbin/sshd -D
2 root 3407 0.0 0.0 112680 988 pts/0 S+ 08:41 0:00 grep --color=auto sshd
[root@localhost ~]# ps -aux |grep "sshd"|grep -v grep |cat -n #排除掉grep命令进程本身
1 root 1097 0.0 0.0 105996 220 ? Ss 08:19 0:00 /usr/sbin/sshd -D
[root@localhost ~]# ifconfig |grep inet |head -1|awk '{print $2}' #获取IP地址
192.168.199.129