df换行问题的设置
df是linux下用来查磁盘空间的命令,而在使用了LVM分区或网络挂载的情况下,再用df取分区的使用率时,发现有些分区显示换行了,这样会导致通过脚本取的数据不对。
1[root@ ]# df -h
2Filesystem Size Used Avail Use% Mounted on
3/dev/sda5 154G 6.1G 140G 5% /
4/dev/sda2 29G 1.1G 26G 5% /opt
5/dev/sda1 190M 12M 169M 7% /boot
6tmpfs 3.9G 0 3.9G 0% /dev/shm
7/dev/sdb1 2.5T 113G 2.3T 5% /mail
8/dev/mapper/vg_01-LogVol00
9 68G 180M 64G 1% /App
10218.85.xxx.xxx:/opt/apacche/conf
11 59G 218M 56G 1% /opt/apacche/conf
而直接使用awk进行截取数据时,可能就出现下面的情况:
1[root@web45 data1]# df -h|awk '{print $1,$5}'
2文件系统 已用%%
3/dev/sda2 3%
4tmpfs 0%
5/dev/mapper/vg_01-LogVol00
668G /App
7/dev/sda1 29%
8/dev/mapper/vg_01-LogVol02
9385G /data1
10/dev/mapper/vg_01-LogVol03
11401G /data2
12/dev/mapper/vg_01-LogVol01
1373G /logs
解决该问题的方法也十分简单,在使用df命令时加上参数:-P就可以解决。-P –portability use the POSIX output format
1[root@ ]# df -hP|awk ‘{print $1,$5}’
2Filesystem Use%
3/dev/sda5 5%
4/dev/sda2 5%
5/dev/sda1 7%
6tmpfs 0%
7/dev/sdb1 5%
8218.85.xxx.xxx:/opt/apacche/conf 1%
而如果想让输出的结果而美观一些,可以再通column命令配合进行输出:
1[root@web45 data1]# df -hlP|awk '{print $1,$5}'|column -t
2文件系统 已用%%
3/dev/sda2 3%
4tmpfs 0%
5/dev/mapper/vg_01-LogVol00 1%
6/dev/sda1 29%
7/dev/mapper/vg_01-LogVol02 32%
8/dev/mapper/vg_01-LogVol03 1%
9/dev/mapper/vg_01-LogVol01 1%
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/df/58.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.