You're viewing a comment by Peter Krumins and its responses.

October 07, 2008, 06:20

Sydney, thanks for your question.

I see a couple of mistakes here. First of all you are using grep twice! Awk can do what grep does itself with the /.../ regex pattern matching.

Here is what I came up with (works in GNU Awk only!):

df -h | awk '/dev/ { if (strtonum($5) >= 20) { print $NF ": " $(NF-1) } }'

And this one works in all Awk's:

df -h | awk '/dev/ {
 if (match($5, /^[0-9]+/)) {
  usage = substr($5, RSTART, RLENGTH)
  if (usage >= 20) {
   print $NF ": " $(NF-1)
  }
 }
}'

Hope it helps.

Comment Responses

Lee Permalink
March 28, 2016, 20:32

If you add 0 to a field it's interpreted as a number. So

df -h | awk '/dev/ { if (strtonum($5) >= 20) { print $NF ": " $(NF-1) } }'

can be made to work in all awk's by

df -h | awk '/dev/ { if (($5 + 0) >= 20) { print $NF ": " $(NF-1) } }'

Regards,
Lee

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "unix_92": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.