Operating System - Linux
1828346 Members
3109 Online
109976 Solutions
New Discussion

Re: How to compare 2 numbers from a string

 
SOLVED
Go to solution
Carles Viaplana
Valued Contributor

How to compare 2 numbers from a string

Hello,

I need to check filesystem used and free space, so I want to get percentage and compare it with a value.

Script I'm using doesn't work fine in some cases due data I get from system is a string (ie, in case of percentage is 9, for my script it's greater than 85):

bdf -l|tail +2|awk 'substr($5,1,length($5)-1) > 85 && $6 == "/FAR/oracle/arch" {print}'

Is there any way to change format to an integer or to change ">" for any other command?

Thanks in advance,

Carles
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: How to compare 2 numbers from a string

Hi Carles:

You can force 'awk' to intergize by adding zero. Try this:

bdf -l|tail +2|awk '{n=substr($5,1);n=n+0;if (n > 85 && $6 == "/FAR/oracle/arch" {print}}'

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: How to compare 2 numbers from a string

Hi (again):

Oops, I dropped the chop of the "%":

# bdf -l|tail +2|awk '{n=substr($5,1,(length($5)-1));n=n+0;if (n > 85 && $6 == "/FAR/oracle/arch" {print}}'

You could also eliminate the extra process (the 'tail':

# smhmso # bdf -l|awk '{NR>2;n=substr($5,1,($length($5)-1));n=n+0;if (n > 85 && $6 == "/FAR/oracle/arch" {print}}'

Regards!

...JRF...
Regards!

...JRF...


Hein van den Heuvel
Honored Contributor

Re: How to compare 2 numbers from a string


I would let awk do more work:

bdf -l| awk '/arch$/{sub(/%/,"",$5); if ((0+$5)>85){print}}'

Here tail is replaced by awk just lookinf for a line ending in 'arch'.
You can extent that to \/FAR\/oracle\/arch$ if need be.
It then replaces the % in $5 with nothing, converts to numeric and compares.

fwiw,
Hein.
Sandman!
Honored Contributor

Re: How to compare 2 numbers from a string

Is the output of bdf -l getting wrapped onto another line (happens when the name of the devices is long)???

If that's the case then your script will not be able to parse, compare and print the filesystems that are exceeding your "> 85" criteria. You need to modify your script as:

# bdf -l | awk 'NR!=1 && NF!=1 && substr($(NF-1),1,length($(NF-1))-1) > 85 && $6 == "/FAR/oracle/arch" {print}'
Sandman!
Honored Contributor

Re: How to compare 2 numbers from a string

Hello...small change to the previous posting; change the $6 with $NF i.e.

# bdf -l | awk 'NR!=1 && NF!=1 && substr($(NF-1),1,length($(NF-1))-1) > 85 && $NF == "/FAR/oracle/arch" {print}'

regards!
Bill Hassell
Honored Contributor

Re: How to compare 2 numbers from a string

As mentioned, bdf is unstable when it comes to the lines of data. If you use bdf directly, your script will fail with long device filenames or NFS mountpoints. It is a very common task so I've attached a useful diskspace monitoring script with configurable % per mountpoint *and* a notification only once per limit, with another notice when the usage increases a specified amount over the limit.


Bill Hassell, sysadmin
Alan Meyer_4
Respected Contributor

Re: How to compare 2 numbers from a string

Here's a perl script I wrote to force bdf into 1 line per filesystem. The problem referenced in Bills post.
" I may not be certified, but I am certifiable... "
Carles Viaplana
Valued Contributor

Re: How to compare 2 numbers from a string

I applied solution provided by James and it works fine.

Thanks to all!