954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Converting percentage to a number

Hello,

I am trying to use the awk command to print two columns. One of them has a percentage value. This is for file system monitoring. When i try to read it from a file it does not recognize the precent sign and ignores it. How can i convert it or skip the % sign when using this command?

Any help would be appreciated.

Thanks.
Shawn

#! /usr/bin/ksh
HOST=`hostname`
cd /home/orabp1/scripts

/usr/bin/df -g | grep -i /bp1db01_bk|awk '{print $7" "$4}' > fs_usage
if [ `cat fs_usage |awk '{print $2}` -gt 36 ]
then
mailx -s "Backup Filesystem Usage in $HOST has exceded" [email]acvd@drede.com[/email] < fs_usage
fi


bckp_chk.sh[7]: 35%: 0403-012 A test command parameter is not valid

shawnk
Newbie Poster
7 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 
...
/usr/bin/df -g | grep -i /bp1db01_bk|awk '{sub (/%/, "", $7); print $7" "$4}' > fs_usage
...

Adding a sub() statement to your awk code that deletes the % from the df() output should work. The above syntaxshould work on your system (AIX & Pains or Solaris?); I'm still using a 1988 version of AT&T's AWK book.

Your code is sub-optimal, but that don't matter because it isn't running 1,000,000 times a second. It'll work.

Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
 

That worked perfect but now i get this error msg for the next line.
My dumb brain can't figure out how to fix it :(

if [ `cat fs_usage |awk '{print $2}` -gt 36 ]

I tried to change it and put another quotation mark but it didn't solve the issue.

"0403-012 A test command parameter is not vaild."

shawnk
Newbie Poster
7 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

That worked perfect but now i get this error msg for the next line. My dumb brain can't figure out how to fix it :(

if [ `cat fs_usage |awk '{print $2}` -gt 36 ]

I tried to change it and put another quotation mark but it didn't solve the issue.

"0403-012 A test command parameter is not vaild."

That's what happens when you've been looking at the problem too long. I'm going through the same thing now on my own project ('updating' Smoothwall Express to use LFS 6.4). Boy am I overlooking stuff that doesn't become obvious very quickly! :( :(

Your awk code is missing the closing apostrophe. And you'll probably find $1 has the numeric value to compare.

Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
 

That's what happens when you've been looking at the problem too long. I'm going through the same thing now on my own project ('updating' Smoothwall Express to use LFS 6.4). Boy am I overlooking stuff that doesn't become obvious very quickly! :( :(

Your awk code is missing the closing apostrophe. And you'll probably find $1 has the numeric value to compare.


Thanks for your help previously.

shawnk
Newbie Poster
7 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You