第三周作业:sed练习
1、用sed取出df -h显示出的/dev开头的已用磁盘的数据,带单位。
如 红框内容为所提取数据

QQ图片20210406224158.jpg
[root@centos8 ~]# df -h | sed -nr '/^\/dev\/sd/s#.*([ ]+[0-9.]+[GM])([ ]+[0-9.]+[GM]).*#\1#p'
1.5G
33M
132M
[root@centos8 ~]# df -h |sed -nr '/^\/dev/{s#(\S+)(\s+)([0-9.]+[GM][ ]+)([0-9.]+[GM])(.*)#\4#p}'
1.5G
33M
132M
2、用sed取出free -h 剩余空间内容,不带单位(假定单位都是Gi)
如下图红框中的内容 为 所提取的内容

QQ图片20210406224235.jpg
[root@centos8 ~]# free -h |awk -F" " '/^.*:/{print $4}'|sed -nr 's/([0-9.]+).*/\1/p'
726
2.0
[root@centos8 ~]# free -h |sed -nr '/^.*:/{s#(\S+)(\s+)(([0-9.]+)(\w+\s+){2})([0-9.]+)(.*)#\6#p}'
726
2.0
[root@centos8 ~]# free -h | sed -nr 's#(\S+)(\s+)(([0-9.]+)(\w+\s+){2})([0-9.]+)(.*)#\6#p'
726
2.0