1834130 Members
2394 Online
110064 Solutions
New Discussion

Re: awk Help

 
SOLVED
Go to solution
Nobody's Hero
Valued Contributor

awk Help

Hello,
I want to test a command to see if the percentage is less than or greate than. It seems the % sign is giving me an error. Is there a way to test percentage rather than just a number?

if [[ $(swapinfo -tm | grep -i localfs | awk '{ print $5 }') -lt 80% ]]; then
echo " Filesystem Swap Is Cool "
exit
fi

UNIX IS GOOD
9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: awk Help

Robert,

How about adding a sed step to remove the percent sign?

swapinfo -tm | grep -i memory | awk '{ print $5 }' |sed 's/%//'


Pete

Pete
Nobody's Hero
Valued Contributor

Re: awk Help

Thanks Pete, I thought of that, but I was wondering if I can pass that entire expression of percentage to do a comparison.
Thanks anyway.
UNIX IS GOOD
Leif Halvarsson_2
Honored Contributor

Re: awk Help

Hi,
you can perhaps add a second |
something like
| awk '{ print $5 }') |tr -d % -lt 80
Leif Halvarsson_2
Honored Contributor
Solution

Re: awk Help

Sorry,

This should be better:

if [[ $(swapinfo -tm | grep -i localfs | awk '{ print $5 }' |tr -d %) -lt 80
Nobody's Hero
Valued Contributor

Re: awk Help

Bingo,

Thanks Leif
UNIX IS GOOD
Pete Randall
Outstanding Contributor

Re: awk Help

Just curious: how is using tr so different from using sed?


Pete

Pete
Nobody's Hero
Valued Contributor

Re: awk Help

Your correct, no difference, and still after running the command on the line, it still isnt calculating percentage. My bad.

Other than the fact it was my mistake, I hate sed. I dont know why, but I do.

Right, no difference, still not evaluating true percentage value.
UNIX IS GOOD
Fred Martin_1
Valued Contributor

Re: awk Help

I do a ping to our remote locations every so often, and check to see if the number of packets dropped is higher than 10%.

I do it like this:

ping fradban -n 5 | grep 'packet loss' | awk '{print $7}' | sed 's/\%//g'

The result is the numerical part of the percentage dropped.

I store that in a variable called drp.

Then I test that:


if [ "$drp" = "" ]
then
drp = "99999"
fi
if [ "$drp" -gt "10" ]
then
# page someone on IT staff
fi

fmartin@applicatorssales.com
c_51
Trusted Contributor

Re: awk Help

why call three programs when one will do:

num=$(swapinfo -tm | awk '/localfs/ {sub("%","",$5);print $5;}')
if (( $num < 80 )) ;then
...