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.
It should return 85 from the input
/dev/disk1 112Gi 94Gi 18Gi 85% 24699942 4621784 84% /
This will fix the command as you supplied it.df -hl | grep '/dev/disk1' | awk '{sub( /%/, ""); print $5 }'
No need to escape the double quotes.
Of course you don't need to use grep here either.df -hl | awk '/disk1/ { sub( /%/, "", $5); print $5}'
Notice that you can supply the target for the substitution as a third argument to sub
.
The sub command is described in the gawk manual on this page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With