gpt4 book ai didi

linux - awk 在第 1 列中打印大于零的行

转载 作者:太空狗 更新时间:2023-10-29 11:31:43 25 4
gpt4 key购买 nike

我正在尝试打印可用空间大于零的目录名称。

  #Listed all the directory and their consumed space.
du -sm storage*
365 storage1
670 storage2
1426 storage3

我的阈值为 1000M ,因此我尝试打印这些目录中相对于提供的阈值的可用空间。

du -sm storage* | awk -v threshold="1000" '$1>0{print $1=threshold-$1,$2}'
635 storage1
330 storage2
-426 storage3

所以,我想打印那些自由大小为正整数的目录。像这样的东西:

635 storage1
330 storage2

有什么更正吗?

最佳答案

可以这样写,

awk -v threshold="1000" '{$1=threshold-$1} $1 > 0'

示例

awk -v threshold="1000" '{$1=threshold-$1} $1 > 0' input
635 storage1
330 storage2

它有什么作用?

  • $1=threshold-$1 设置相对于阈值的第一列。

  • $1 > 0 检查导出的第一列是否大于零。如果此表达式的计算结果为真,它会打印整个输入行。

关于linux - awk 在第 1 列中打印大于零的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37811899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com