awk problems - awk ignores conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk problems - awk ignores conditions
# 1  
Old 12-27-2013
awk problems - awk ignores conditions

Code:
 awk 'BEGIN{
       if('"$CATE"'<'"${WARN}"')
               printf ("%s", "'"`Kfunc "" ; break`"'")
       else if (('"${CATE}"'>='"${WARN}"') && ('"${CATE}"'<'"${CRIT}"'))
               printf ("%s", "'"`Wfunc ""; break`"'")
       else if ('"${CATE}"'>='"${CRIT}"')
               printf ("%s", "'"`Cfunc "" ; break`"'")
       else
               printf ("%s", "'"`Ufunc "" ; break`"'")
'

when i run the above, it appears it is always running all four functions, despite the "else if" conditions i specified.

what am i doing wrong here?

Last edited by SkySmart; 12-27-2013 at 05:16 PM..
# 2  
Old 12-27-2013
1. awk expects input from a file, no filename given means it waits for input from stdin
2. Do not try to use shell variables that way

Code:
awk -v currentstate="$CURRENTSTATE" '{ if (currentstate=="foo" ) {exit} }'

Each shell variable needs to be "translated" for awk using -v [awk variable] ="$shell_variable"
# 3  
Old 12-27-2013
Quote:
Originally Posted by jim mcnamara
awk expects input from a file, no filename given means it waits for input from stdin
An awk program doesn't require an input file if the program has only BEGIN rule and no other rule. The program exits right after the BEGIN rule is run.
# 4  
Old 12-27-2013
Quote:
Originally Posted by jim mcnamara
1. awk expects input from a file, no filename given means it waits for input from stdin
2. Do not try to use shell variables that way

Code:
awk -v currentstate="$CURRENTSTATE" '{ if (currentstate=="foo" ) {exit} }'

Each shell variable needs to be "translated" for awk using -v [awk variable] ="$shell_variable"
thanks jim. the awk script actually runs, it just doesn't do what i expect it to do.

if an awk script isn't reading its information from a file, as i understand it, you can pass variables to it one of two ways, the way you described, and the way i used.

my problem is, i need to run some custom shell functions if a condition specified in awk is met. when i run the script, it runs all the functions for all four conditions.

the custom shell functions are the functions between "``"
# 5  
Old 12-27-2013
Code:
 awk 'BEGIN{
       if('"$CURRENTSTATE"'<'"${WARNING}"')
               printf ("%s", "'"`MyOKFunc "" ; break`"'")
       else if (('"${CURRENTSTATE}"'>='"${WARNING}"') && ('"${CURRENTSTATE}"'<'"${CRITICAL}"'))
               printf ("%s", "'"`MyWARNFunc ""; break`"'")
       else if ('"${CURRENTSTATE}"'>='"${CRITICAL}"')
               printf ("%s", "'"`MyCRITFunc "" ; break`"'")
       else
               printf ("%s", "'"`MyUNKFunc "" ; break`"'")
'

If you thought that awk was going to perform the shell command substitutions when the awk printf statements are executed, you are wrong. The shell has to perform all four of those command substitutions to put together the parameters that will be passed to awk as the program awk is to evaluate. Furthermore, the shell break statements in all four of those command substitutions produce undefined results; break only has defined meaning inside a shell for, while, or until loop. If you are including this awk statement in the middle of a shell for, while, or until loop and hoping that the breaks in the command substitutions in the awk printf arguments will break out of that shell loop; that won't happen either. And, if that isn't enough, you don't have a valid awk program even if the command substitutions yielded syntactically correct awk printf statements; there is no closing '}' to match the '{' at the start of your awk BEGIN clause.

Even if you had a valid awk program and had valid command substitutions, assuming that the shell variables CRITICAL, CURRENTSTATE, and WARNING have been assigned integral values, it would be thousands of times faster (and MUCH easier to read and understand) if you would write this using shell if statements rather than invoking awk so you can use awk's < and >= operators (instead of using test's -lt and -ge primaries) and awk's more relaxed spacing requirements.

Last edited by Don Cragun; 12-27-2013 at 05:52 PM.. Reason: Removed incorrect statement about needing to quote the strings produced by the shell functions.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 12-27-2013
Quote:
Originally Posted by Don Cragun
Code:
 awk 'BEGIN{
       if('"$CURRENTSTATE"'<'"${WARNING}"')
               printf ("%s", "'"`MyOKFunc "" ; break`"'")
       else if (('"${CURRENTSTATE}"'>='"${WARNING}"') && ('"${CURRENTSTATE}"'<'"${CRITICAL}"'))
               printf ("%s", "'"`MyWARNFunc ""; break`"'")
       else if ('"${CURRENTSTATE}"'>='"${CRITICAL}"')
               printf ("%s", "'"`MyCRITFunc "" ; break`"'")
       else
               printf ("%s", "'"`MyUNKFunc "" ; break`"'")
'

If you thought that awk was going to perform the shell command substitutions when the awk printf statements are executed, you are wrong. The shell has to perform all four of those command substitutions to put together the parameters that will be passed to awk as the program awk is to evaluate. Furthermore, the shell break statements in all four of those command substitutions produce undefined results; break only has defined meaning inside a shell for, while, or until loop. If you are including this awk statement in the middle of a shell for, while, or until loop and hoping that the breaks in the command substitutions in the awk printf arguments will break out of that shell loop; that won't happen either. And, if that isn't enough, you don't have a valid awk program even if the command substitutions yielded syntactically correct awk printf statements; there is no closing '}' to match the '{' at the start of your awk BEGIN clause.

Even if you had a valid awk program and had valid command substitutions, assuming that the shell variables CRITICAL, CURRENTSTATE, and WARNING have been assigned integral values, it would be thousands of times faster (and MUCH easier to read and understand) if you would write this using shell if statements rather than invoking awk so you can use awk's < and >= operators (instead of using test's -lt and -ge primaries) and awk's more relaxed spacing requirements.
thank you. the shell if statements dont handle numbers like this "73.39", which is why i decided to use awk. its either awk or perl. and i personally dont like perl.

i'll retry with the closing brackets. dont know how i missed that. and by the way, yes, i am including this in a while loop. the original awk program i wrote didnt' have the breaks in it. but since it look like awk was running all four conditions instead of just one, i decided to put the break in there to try to force it break out of the first condition it matches.
# 7  
Old 12-27-2013
Quote:
Originally Posted by SkySmart
thank you. the shell if statements dont handle numbers like this "73.39", which is why i decided to use awk. its either awk or perl. and i personally dont like perl.

i'll retry with the closing brackets. dont know how i missed that. and by the way, yes, i am including this in a while loop. the original awk program i wrote didnt' have the breaks in it. but since it look like awk was running all four conditions instead of just one, i decided to put the break in there to try to force it break out of the first condition it matches.
Some shell if statements don't handle floating point comparisons. If you have a 1993 or later ksh, it is perfectly capable of handling double precision floating point arithmetic in arithmetic expressions and in numeric comparisons.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sequence of conditions awk

hello gurus, I want to use an associative array from a file to populate a field of another file, by matching several columns in order of priority. If the first column matches, then i dont want to match $2. Similarly I only want to match $3 when $1 and $2 are not in associative array. For the... (6 Replies)
Discussion started by: ritakadm
6 Replies

2. Shell Programming and Scripting

If conditions in awk

Hello Friends, I need to find some CDRs in production servers whose 1st field value and 2nd field value = 1 and 11th looks like 45.123... where there are more than 3 digits after comma.so i wrote a one liner, something like below but does not work, however when i used first and second conditions... (8 Replies)
Discussion started by: EAGL€
8 Replies

3. Shell Programming and Scripting

awk three conditions

I'm having a problem pulling UID's from data. The data outputs a user's UID in one of three ways: 1. Error User user_name already assigned with <UID> 2. Success <UID> reserved for user_name 3. <a load of crap because there was a db failure yet somehow the UID is still in there> I typically... (5 Replies)
Discussion started by: MaindotC
5 Replies

4. Shell Programming and Scripting

awk with conditions

Hi All, I have a file with below contents. "en2"/10.185.81.0:cluster_interconnect,"en5"/10.185.81.0:cluster_interconnect,"en6"/169.181.146.0:public I want to take the interface name from the file and convert it as ipaddress using ifconfig command get the output like below en6 ->... (2 Replies)
Discussion started by: kamauv234
2 Replies

5. Shell Programming and Scripting

Conditions in awk

Hi there, here is my command ssh host.local "/path/to/my/perscript/hostconfig.pl -s $HOST -d |awk '{if (\$4 > 120)print \"My error message\";exit}{s=0; for (i=1; i<=NF; i++) s++; if(s == 13) print \$3}'" The problem is if conditional 1 is met (i.e $4 > 120), i don't see "My error message", the... (5 Replies)
Discussion started by: urello
5 Replies

6. Shell Programming and Scripting

Checking conditions with AWK

Input File1 0BB2 2A11 Split,FriApr80625,1507_7RAID5 0BF6 2829 Synchronized,FriJan140653,1507_7RAID5 0BF6 282A Split,FriApr80625,1507_7RAID5 0C7C 199E Synchronized,FriJan140653,1507_7RAID5 0C7C 1BCC Split,FriApr80625,1507_7RAID5 0DCA 0A9B ... (12 Replies)
Discussion started by: greycells
12 Replies

7. Shell Programming and Scripting

awk with two conditions

Hi Everyone, # cat 1 1;2;3;4;5;6 1;2;3;4;5; # awk -F ";" '$5 == "5"' 1 1;2;3;4;5;6 1;2;3;4;5; but the output is should be just "1;2;3;4;5;6" means 1st condition: $5 is 5; 2nd condition: $6 is not empty, please advice. Thanks (2 Replies)
Discussion started by: jimmy_y
2 Replies

8. Shell Programming and Scripting

awk ignores fields with only spaces or empty

Hi, Does any one know how to avoid the scenario where awk ignores the fields having only spaces or empty fields? for instance, Data: "a","b","c","d",""," " code: awk -F, '{ print NF }' File the output I get is 4 instead of 6 do you know how to avoid this? (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies

9. Shell Programming and Scripting

specifying multiple conditions in AWK

how can i specify more than 1 consition in the following AWK statament?? i.e. if $2 is ABCD and $3 is MNOP and $4 is KLPM similarly for OR #!/bin/ksh awk -F '' ' $2 == "ABCD" { print $2, $3;}' file.xml (2 Replies)
Discussion started by: skyineyes
2 Replies

10. Shell Programming and Scripting

About awk conditions

Hello, Can you explain why in the first 2 commands the awk does not print anything? Is it looking of a specific format ? Thanks. $ echo 12a3 | awk '($1>=2) {print $1}' # prints nothing $ echo 123a | awk '($1>=2) {print $1}' # prints nothing $ echo a123 | awk '($1>=2) {print $1}' a123... (1 Reply)
Discussion started by: majormark
1 Replies
Login or Register to Ask a Question