简体   繁体   English

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

[英]awk to print line greater than zero in column 1

I am trying to print the name of directories whose free space is greater than zero. 我正在尝试打印其可用空间大于零的目录的名称。

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

I have threshold value of 1000M , so I am trying to print free space in these directories relative to threshold value provided. 我的阈值为1000M,因此我尝试在这些目录中相对于提供的阈值打印可用空间。

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

So , I want to print those directories whose free size is positive integer. 因此,我想打印其自由大小为正整数的那些目录。 Something like : 就像是 :

635 storage1
330 storage2

Any correction ? 有更正吗?

You can write it like this, 你可以这样写

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

Example

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

What it does? 它能做什么?

  • $1=threshold-$1 Sets the first column relative to the threshold. $1=threshold-$1设置相对于阈值的第一列。

  • $1 > 0 Checks if the derived first column is greater than zero. $1 > 0检查派生的第一列是否大于零。 If this expression evaluates true, it prints the entire input line. 如果此表达式的计算结果为true,则将打印整个输入行。

I think this got overly complicated. 我认为这太过复杂了。 If you just want to check that the size is positive and lower than a given threshold, say so: 如果您只想检查大小是否为正且小于给定阈值,请这样说:

awk -v threshold=1000 '0 < $1 && $1 < threshold'

Test 测试

$ cat file
635 storage1
330 storage2
-426 storage3

$ awk -v thr=1000 '0 < $1 && $1 < thr' file
635 storage1
330 storage2

Just check $1 > 0 in awk 只需检查$1 > 0 in awk

du -sm storage*|awk '{if ( $1 > 0 ) print }'

or 要么

du -sm storage*|awk  '$1>0'

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

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