Why is my awk helper command not working?

When i started

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

I get the following error:

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

I cannot find any awk sub documentation.

df -hl | grep '/dev/disk1'

returns

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

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

It should return 85 from input

/dev/disk1                         112Gi   94Gi   18Gi    85% 24699942 4621784   84%   /
+4
source share
2 answers

This will correct the team as it is delivered.
df -hl | grep '/dev/disk1' | awk '{sub( /%/, ""); print $5 }' There is no need to avoid double quotes.

Of course, you do not need to use grep here.
df -hl | awk '/disk1/ { sub( /%/, "", $5); print $5}'

Note that you can set a substitution target as the third argument sub.

The subcommand is described in the gawk manual on this page.

+5
source

, df awk :

df --output=pcent /dev/disk1 | awk '/ /{printf("%d\n", $1)}'
+4

All Articles