Percentage sign causing awk problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Percentage sign causing awk problems
# 1  
Old 12-13-2015
Percentage sign causing awk problems

looks like awk gets confused when there's a % next to a number.

command im running:
Code:
awk -F" " '/phxnaz001b/ && /vol/ && NF { if (($NF >= 80) && ($NF < 83)) { print ; print ; w++ } else if ($NF >= 83) { print ; c++ } } END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c) }' /tmp/test.log

contents of /tmp/test.log

Code:
phxnaz001b      /vol/NFS_nagios/       2148GB    190GB    1957GB          9%
phxnaz001b      /vol/RCC01PLM_COPY/       5734GB    4738GB    996GB          83%
phxnaz001b      /vol/RCC01PLM_COPY/       5734GB    4738GB    996GB          83%
phxnaz001b      /vol/PHX01PLM/       9146GB    7462GB    1683GB          82%
phxnaz001b      /vol/PHX01PLM/       9146GB    7462GB    1683GB          82%
phxnaz001b      /vol/nfs_datastore03/       593GB    55GB    538GB          9%
phxnaz001b      /vol/electricCommander/       3673GB    3193GB    479GB          87%
phxnaz001b      /vol/NFS_nagios/       2148GB    190GB    1957GB          9%
phxnaz001b      /vol/RCC01PLM_COPY/       5734GB    4738GB    996GB          83%
phxnaz001b      /vol/RCC01PLM_COPY/       5734GB    4738GB    996GB          83%

it looks like the awk is thinking 9% means 90%. which is not the case. how can i modify this awk command to accurate grab the right lines from the log?
# 2  
Old 12-13-2015
Substitute $NF for $NF+0

---------- Post updated at 11:06 PM ---------- Previous update was at 10:18 PM ----------

If you do not mind, allow me to mention a few things about your snipet.
Code:
awk -F" " '/phxnaz001b/ && /vol/ && NF { if (($NF >= 80) && ($NF < 83)) { print ; print ; w++ } else if ($NF >= 83) { print ; c++ } } END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c) }' /tmp/test.log

-F" " : The default is already white space, making this unnecessary, unless you want to exclude tabs which in this case it not relevant.
/phxnaz001b/ : Since this is the first field $1=="phxnaz001b" can be used instead to directly compare for the string, avoiding the regex lookup match, which is more expensive.
/vol/ : this will try to match in the whole record or $0, which is more expensive than pointing directly to the field number i.e. $2 ~ /\/^vol/, also by using the anchor ^, it doesn't have to search the whole field nor will it match in the middle if found.
&& NF : Not necessary, it will always be true if any of the other expressions evaluate to true.
print ; print: Not clear why you are displaying the same line twice if the range is between 80 and 82 inclusive.
"%d:OK : Always 0 since you are not keeping tally of what falls outside the warning or critical level.

Changing those elements, the result would be something like:
Code:
awk '$1=="phxnaz001b" && $2 ~ /^\/vol/ {if($NF+0 >= 80 && $NF+0 < 83){print; w++} else if($NF+0 >= 83){ print; c++ } else {print; o++}} END {printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c)}' test.log

or removing the individual prints:

Code:
awk '$1=="phxnaz001b" && $2 ~ /^\/vol/ {print; if($NF+0 >= 80 && $NF+0 < 83)w++; else if($NF+0 >= 83)c++; else o++} END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c)}' test.log


Last edited by Aia; 12-13-2015 at 06:38 PM.. Reason: Explicit at id est
This User Gave Thanks to Aia For This Post:
# 3  
Old 12-13-2015
Awk doesn't take % as a number. It is considered as a string. Try this:

awk '{if ((substr($NF,1,length($NF)-1)+0)>80) print}' test.log

Here you convert $NF which is a percentage (string) to a number by extracting the number from it. substr function still gives you a string. To convert it to a decimal you add 0 to it.
This User Gave Thanks to zeus101 For This Post:
# 4  
Old 12-13-2015
Quote:
Originally Posted by zeus101
Awk doesn't take % as a number. It is considered as a string. Try this:

awk '{if ((substr($NF,1,length($NF)-1)+0)>80) print}' test.log

Here you convert $NF which is a percentage (string) to a number by extracting the number from it. substr function still gives you a string. To convert it to a decimal you add 0 to it.
Adding a number to a string causes awk to use the characters at the start of that string that specify a numeric value (or zero if there isn't a leading number) to the number. Using substr() to strip off trailing non-digits isn't necessary.

Code:
printf '%s\n' 99.44% 1E2percent infinity nonumber | awk '{print $1+0}'

produces something like:
Code:
99.44
100
inf
0

(The string used to represent infinity in awk output varies from system to system (usually inf, infinity, Inf, or Infinity), but any input string starting with a case-insensitive inf should be interpreted by awk to be an infinite value when it is being evaluated as a number. And, as shown by the string starting with 1E2, awk also recognizes exponential numeric formats.)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. HP-UX

Shell /awk script for Percentage

having two columns, A and B.. i need to add another column C in a file and calculate the percentage based on the column A and B. (COLUMN B/ COLUMN A *100) . "|" is delimiter separating the A and B.. need C column with the percentage value. Thanks for your help 100|50 |50% ... (6 Replies)
Discussion started by: kartikirans
6 Replies

2. Shell Programming and Scripting

find percentage - awk

Please help me with this ... Input file /vol/test1 10G /vol/test2 1G /vol/test3 200G /vol/test4 3G Output File /vol/test1 10G - - 9G - /vol/test2 1024M - - 921M - /vol/test3 200G - - 180G - /vol/test4 3072M - - 2764M - Basically if Column 2 ( which is... (6 Replies)
Discussion started by: greycells
6 Replies

3. Shell Programming and Scripting

Spaced input causing awk error

Hi all, Just want to say thanks for the great forum you have here, the old topics and posts have helped tremendously. So much so that I have managed to figure a lot out just by researching. However, I'm having a small issue that I simply can't find the answer to. (4 Replies)
Discussion started by: whyte_rhyno
4 Replies

4. Shell Programming and Scripting

awk/sed percentage calculation

Hi all i have a text file with columns delimited with , 2010-08-18,10,24,.09751,39,7,14872,26732 . . . i would to add a extra column in the end with percentage calculation of columns 5 and 8 ie (39/26732)*100 so the output must look like ... (6 Replies)
Discussion started by: posner
6 Replies

5. Shell Programming and Scripting

Need an AWK script to calculate the percentage

Hi I need a awk script to calculate percentage. I have to pass the pararmeters in to the awk script and calculate the percentage. Sum = 50 passed = 43 failed = 7 I need to pass these value in to the awk script and calculate the percentage. Please advice me. (8 Replies)
Discussion started by: bobprabhu
8 Replies

6. Shell Programming and Scripting

awk percentage

how would you calculate percentage by per line? Given a column of 16 lines, grab each line and divide it by the sum of the entire column and multiply by 100? thanks ... (8 Replies)
Discussion started by: rockiefx
8 Replies

7. UNIX for Dummies Questions & Answers

GCC causing problems it seems.

Hi, I seem to be getting errors in relation to GCC it seems as I cant upgrade alot of pkgs until I can upgrade or use a later version of GCC. The error I get is along the lines of ( cc1: error: unrecognized command line option "-Wno-pointer-sign" *** Error code 1 ) Anyway I was wondering if... (2 Replies)
Discussion started by: Browser
2 Replies

8. Post Here to Contact Site Administrators and Moderators

HTML is causing problems

I have to suggest that we turn HTML back off. The problem is that angle brackets are used in code and this is causing stuff to get dropped from posts. I know that we can use the constructs that PxT mentions in this thread. But look how hard it is to educate folks about code tags and the search... (4 Replies)
Discussion started by: Perderabo
4 Replies

9. UNIX for Advanced & Expert Users

percentage sign in a drive mapping ?

Good day all, I'm hoping someone can help me understand what the percentage sign is and does in mapping a drive to a server ? I provided the example for you. (ie \\server1\share%simon) thanks simon2000 (2 Replies)
Discussion started by: simon2000
2 Replies
Login or Register to Ask a Question