trying to remove percentage symbol '%' from this shell script
df -h | awk '$NF=="/"{printf "Percentage: %s \n", $5}'
output:
Percentage: 2%
want to make the output like this
Percentage: 2
sorry for my bad english
trying to remove percentage symbol '%' from this shell script
df -h | awk '$NF=="/"{printf "Percentage: %s \n", $5}'
output:
Percentage: 2%
want to make the output like this
Percentage: 2
sorry for my bad english
Force the interpretation of the value in $5
as a numeric value. This can be done by adding zero to the value before printing it using the %s
format:
df -h | awk '$NF == "/" { printf "Percentage: %s\n", 0+$5 }'
Or, by using %d
to format the value as a decimal integer rather than as a string:
df -h | awk '$NF == "/" { printf "Percentage: %d\n", $5 }'
Or, explicitly delete the last character from the string before printing:
df -h | awk '$NF == "/" { printf "Percentage: %s\n", substr($5,1,length($5)-1) }'
df -h | awk '$NF == "/" { sub(".$","",$5); printf "Percentage: %s\n", $5 }'
Or, simply remove all %
characters in the input:
df -h | tr -d % | awk '$NF == "/" { printf "Percentage: %s\n", $5 }'
df -h | awk '{ gsub("%","") } $NF == "/" { printf "Percentage: %s\n", $5 }'
And one more way to remove the %
symbol:
df -h | awk '$NF == "/" { printf "Percentage: %s\n", int($5) }'
awk
function. – QuartzCristal Jul 08 '22 at 18:38