簡體   English   中英

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

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

我正在嘗試打印其可用空間大於零的目錄的名稱。

  #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檢查派生的第一列是否大於零。 如果此表達式的計算結果為true,則將打印整個輸入行。

我認為這太過復雜了。 如果您只想檢查大小是否為正且小於給定閾值,請這樣說:

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

測試

$ cat file
635 storage1
330 storage2
-426 storage3

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

只需檢查$1 > 0 in awk

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

要么

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM