1

When I run

df -hl | grep '/dev/disk1' | awk '{sub(/%/, \"\");print $5}'

I'm getting the following error:

awk: syntax error at source line 1
context is
    {sub(/%/, >>>  \ <<< "\");}
awk: illegal statement at source line 1

I can't seem to find any documentation on awk sub.

df -hl | grep '/dev/disk1'

returns

/dev/disk1                         112Gi   94Gi   18Gi    85% 24672655 4649071   84%   /

As I understand, it should return the percentage of disk space used.

KinsDotNet
  • 197
  • 7

1 Answers1

1

You simply need to remove the pair of backslashes:

df -hl | grep '/dev/disk1' | awk '{sub(/%/, "");print $5}'

Awk sub function is well documented to replace the first occurrence of the pattern passed as first parameter by the string passed as second parameter.

Here the pattern is the percent sign and the replacement string is an empty string. As it is written in your question, the second parameter is an invalid string so awk complains and quit. The corrected awk statement is removing the % appearing in its input and displaying the fifth field.

Note that the grep command is redundant here, as awk is able to do the filtering alone so the command can be simplified into:

df -hl | awk '/\/dev\/disk1/ {sub(/%/, "");print $5}'
jlliagre
  • 8,691
  • 16
  • 36