Linux: Get rid of % when using grep and awk

I use this to trim the % from the end of a string:
x="10%"
echo ${x%\%}

Open in new window


I use this to return results from df in JSON format:
df |grep -iv "filesystem" | awk '{print"\""$1"\":"$5"," }'

Open in new window


How can these two concepts be combined?  I want to remove % from the JSON results.
LVL 16
hankknightAsked:
Who is Participating?
 
woolmilkporcConnect With a Mentor Commented:
No need for "grep".

df | awk '! /[Ff]ilesystem/ {sub("%","",$5); print"\""$1"\":"$5"," }'

"sub" means "substitute". All we have to do is replacing the "%" sign with the empty string "".
wmp
0
 
carlmdCommented:
I don't see a % sign in the result of

df |grep -iv "filesystem" | awk '{print"\""$1"\":"$5"," }'

Can you post an example of what you see?
0
 
woolmilkporcCommented:
I think hankknight's "df" behaves like "df -P" (POSIX format, where the 5th column is "%Used").
0
All Courses

From novice to tech pro — start learning today.