Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my awk sub command failing?

Tags:

shell

unix

awk

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%   /
like image 764
KinsDotNet Avatar asked Jan 07 '23 00:01

KinsDotNet


1 Answers

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.

like image 147
Niall Cosgrove Avatar answered Jan 21 '23 14:01

Niall Cosgrove