RandomUser RandomUser - 5 months ago 24
Linux Question

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.

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 ?

Answer

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 > 0 Checks if the derived first column is greater than zero. If this expression evaluates true, it prints the entire input line.