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

标签 linux bash shell awk scripting

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

  #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/

相关文章:

linux - 在文件内容中搜索单词和日期 - shell 脚本

c - 如何消除 Linux 中 DDA 行算法中的以下错误?

bash - 排序实用程序的字母顺序是什么?

node.js - 使用命令扩展运行 NPM 脚本会在返回结果之前解析结果 - 与 bash 不同

linux - 查询arp表并通过restful API发送的shell脚本?

linux - 我如何从其他目录调用make文件

linux - Hadoop/Hive 命令在 Crontab 中不起作用

android - "No such file or directory"试图在 Android 设备上执行 linux 二进制文件

bash - 从巨大的文件中打印样本

Bash 包自动化/w 操作系统检测