1827321 Members
3915 Online
109961 Solutions
New Discussion

awk and numerical values

 
Joanne Joki_1
Occasional Contributor

awk and numerical values

I have written a little script that goes out and looks for file system sizes that are above a threshold - works great - except when I ask it to lets say 50%, it also shows me 6%, 7%, and so on ... how do I force it to see the value as 50 not 5?
6 REPLIES 6
Michael Tully
Honored Contributor

Re: awk and numerical values

Hi,

I use a simple rendition of this. You could expand it to use what ever parameters you wish.

#
# Simple script to get bdf information over a threshold
#
bdf | sed 's/^ *//' | awk 'NF==1{f=$0;getline;$0=f$0}{print}' | grep -e "8[0-9]%" -e "9[0-9]%" -e "100%"

Cheers
~Michael~
Anyone for a Mutiny ?
Jeff Schussele
Honored Contributor

Re: awk and numerical values

Hi Joanne,

My guess, w/o seeing the script, would be that you're comparing strings & not numerics.
Make sure you use numeric tests - -ne -eq -lt etc.
and not string tests = != -n etc.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
John Carr_2
Honored Contributor

Re: awk and numerical values

Hi

you most probably need to take the string strip the % from it then turn it into a numeric with expr command.

checkut this thread we covered previously

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x6aa08cc5e03fd6118fff0090279cd0f9,00.html

cheers
John.
H.Merijn Brand (procura
Honored Contributor

Re: awk and numerical values

Learn perl, drop awk :)

a5:/ 102 # bdf -l
Filesystem kbytes used avail %used Mounted on
/dev/vg00/lvol3 204800 29232 164610 15% /
/dev/vg00/lvol1 299157 32096 237145 12% /stand
/dev/vg00/wrk 1024000 91636 874296 9% /wrk
/dev/vg00/var 516096 147025 347828 30% /var
/dev/vg00/lvol7 1335296 630974 660352 49% /usr
/dev/vg00/u 1024000 356696 628247 36% /u
/dev/vg00/tmp 409600 16307 369495 4% /tmp
/dev/vg00/pro 12288000 11355578 903970 93% /pro
/dev/vg00/opt 786432 532629 237985 69% /opt
/dev/vg00/home 16384 1109 14328 7% /home
/dev/vg00/data 2048000 634297 1325776 32% /data
a5:/ 103 # bdf -l | perl -ne '/(\d+)%/&&$1>66&&print'
/dev/vg00/pro 12288000 11355594 903954 93% /pro
/dev/vg00/opt 786432 532629 237985 69% /opt
a5:/ 104 #
Enjoy, Have FUN! H.Merijn
Paula J Frazer-Campbell
Honored Contributor

Re: awk and numerical values

Hi

Dont re-invent the wheel.

Script from Andreas Voss Attached.

It will do more than one server.

____________________________________________

#!/sbin/sh
# Check server diskspace
# check with bdf filesystems on multiple servers
# Ensure server is in /etc/hosts file
# (c) hpux@voss2000.de 2000-10-11
# for remote shell there has to be set permission in /.rhosts
# at the remote system(s)
# the script uses /tmp/bdfcheck directory to store percentage of
# each filesystem for history to avoid multiple message generation

MAILUSER="paula@avro.co.uk" # <-- put here the email address for sending
HOSTLIST="N-0 k1 k2" # HIGHWATERMARK=95 # max percentage of filesystem usage

[ ! -d /tmp/bdfcheck ] && mkdir /tmp/bdfcheck

for host in $HOSTLIST
do
{
if [ $host = $(hostname) ]
then
bdf -t hfs -t vxfs -l
else
remsh $host bdf -t hfs -t vxfs -l
fi
}|tail +2 |awk -vhost=$host '{
if(length(fs)>1)
fs=fs $0;
else
{
if(NF == 1)
{
fs=$1;
continue;
}
else
fs=$0
}
printf("%-10s: %s\n",host,fs);
fs="";
}'
done |
awk -vmax=$HIGHWATERMARK -vdate="`date`" 'BEGIN{first=1}
{
percent=$7;
hostname=$1;
filesystem=$NF;
gsub("/","_",filesystem);
sub("%","",percent);
percent=sprintf( "%d",percent);
histfile="/tmp/bdfcheck/" hostname filesystem;
getline oldpercent close(histfile);
print percent >histfile;
line=$0;
if(percent > oldpercent && percent > max)
{
if(first==1)
{
printf("%s\n\nFilesystems with more than %d%% usage:\n \n",date,max);
printf("%-10s Filesystem kbytes used avail %%used Mounted on\n","Hostname");
first=0;
}
printf("%s\n",line);
}
}' >/tmp/bdf_$$

if [ -s /tmp/bdf_$$ ]
then
cat /tmp/bdf_$$ | mailx -s 'FILESYSTEMS WARNING' $MAILUSER
fi
rm -f /tmp/bdf_$$
cd /tmp/bdfcheck
rm *
_____________________________________________


HTH

Paula

If you can spell SysAdmin then you is one - anon
A. Clay Stephenson
Acclaimed Contributor

Re: awk and numerical values

Hi Joanne:

I think I will answer your specific question. The 'trick' in awk is to do something like this:
if (($4 + 0) >= 50) { xxx } else {yyy}

The '+ 0' forces awk to treat the expression as numeric; the reverse operation is to concatenate the null string "" to an expression to force string context. These are standard awk idioms.

Perl has separate comparison operators for numerics and strings to avoid this situation.

Regards, Clay
If it ain't broke, I can fix that.