简体   繁体   English

使用grep输出可用磁盘空间

[英]Output Free Disk Space using grep

I'm trying to create a terminal command which outputs the free disk space of the main HDD. 我正在尝试创建一个终端命令,该命令输出主HDD的可用磁盘空间。 When entering: diskutil info disk1s4 | grep -o "Free Space:.* GB" 输入时: diskutil info disk1s4 | grep -o "Free Space:.* GB" diskutil info disk1s4 | grep -o "Free Space:.* GB" The result is: Free Space: 252.3 GB diskutil info disk1s4 | grep -o "Free Space:.* GB"结果是Free Space: 252.3 GB

I get the correct information but also Free Space: which I don't want. 我获得了正确的信息,但还有Free Space:我不想要的。

Is there a flag I can set which just shows me the results of the wildcard and not the surrounding text ? 我是否可以设置一个标志,仅向我显示通配符的结果,而不显示周围的文本?

I tried Using -A 0 -B 0 to limit the lines. 我尝试使用-A 0 -B 0来限制行数。 removing the -o flag 删除-o标志

The end result should be 252.3 GB . 最终结果应为252.3 GB

You could try awk instead. 您可以改用awk

diskutil info disk1s4 | awk '/Free Space:.* GB/ {print $3}'

Or to get the GB at the end. 或在末尾获得GB

diskutil info disk1s4 | awk '/Free Space:.* GB/ {print $3,$4}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM