进程管道
用法:command1 | command2 |command3 |...
[root@localhost ~]# ll /dev/ |less
[root@localhost ~]# ps aux |grep 'sshd'
[root@localhost ~]# rpm -qa |grep 'httpd' //查询所有安装的软件包,过滤包含httpd的包
[root@localhost ~]# yum list |grep 'httpd'
示例1:将/etc/passwd中的用户按UID大小排序
[root@localhost ~]# sort -t":" -k3 -n /etc/passwd //以: 分隔,将第三列按字数升序
[root@localhost ~]# sort -t":" -k3 -n /etc/passwd -r //逆序
[root@localhost ~]# sort -t":" -k3 -n /etc/passwd |head
-t 指定字段分隔符--field-separator
-k 指定列
-n 按数值
示例2:统计出最占CPU的5个进程
[root@localhost ~]# ps aux --sort=-%cpu |head -6
示例3:统计当前/etc/passwd中用户使用的shell类型
思路:取出第七列(shell) | 排序(把相同归类)| 去重
[root@localhost ~]# awk -F: '{print $7}' /etc/passwd
[root@localhost ~]# awk -F: '{print $7}' /etc/passwd |sort
[root@localhost ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq
[root@localhost ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq -c
131 /bin/bash
1 /bin/sync
1 /sbin/halt
63 /sbin/nologin
1 /sbin/shutdown
-F: 指定字段分隔符
$7 第七个字段
示例4: 统计网站的访问情况
思路: 打印所有访问的连接 | 过滤访问网站的连接 | 打印用户的IP | 排序 | 去重
[root@localhost ~]# netstat -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c
211.156.156.25:80
202.102.15.193:80
222.186.159.252:3000
211.100.40.25:80
123.134.14.159:32806
61.135.11.90:80
202.105.15.193:80
[root@localhost ~]# netstat -an |grep :80 |awk -F: '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n 20
示例5: 打印当前所有IP
[root@localhost ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}'
127.0.0.1
192.168.2.111
示例6:打印根分区已用空间的百分比(仅打印数字)
[root@localhost ~]# df -P |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'
[root@localhost ~]# ip addr |grep 'inet ' |tee ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
192.168.2.102
[root@localhost ~]# cat ip.txt
inet 127.0.0.1/8 scope host lo
inet 192.168.2.102/24 brd 192.168.2.255 scope global eth0
[root@localhost ~]# ip addr |grep 'inet ' |tee -a ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
192.168.2.102