LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Awk: if statement not working (http://www.linuxquestions.org/questions/linux-newbie-8/awk-if-statement-not-working-4175441454/)

shivaa 12-14-2012 10:49 AM

Awk: if statement not working
 
Hello!
Running an if test in awk do not work, but printing all the records. Please have a look if I am making any mistake..
Code:

awk '{if($3 > 0.1); print $3}' sample.txt
File sample.txt contain:
Code:

string1=123 string2=321 0.1000
string1=123 string2=321 0.0100
string1=123 string2=321 0.2000
string1=123 string2=321 0.1100
string1=123 string2=321 0.0050
.......
....... (so on)

But it all the time prints all lines. I tried awk, nawk, but nothing worked.

jpollard 12-14-2012 10:54 AM

I think you want '{if($3 > 0.1) print $3}' ....

Without the ";" in there.

MensaWater 12-14-2012 11:00 AM

It is testing as a string rather than a numeric.

You can force it to test as a numeric by putting a 0 in the variable.

So if you change:
Code:

awk '{if($3 > 0.1); print $3}' sample.txt
to:
Code:

awk '{if($03 > 0.1); print $3}' sample.txt
It will work as you expect.

shivaa 12-14-2012 11:20 AM

Quote:

It is testing as a string rather than a numeric.
@MensaWater: It worked, but why this sort of behaviour? I remember, I had done similar tests earlier, and they were doing exactly what I wanted. For example, if I simply test $3 > 0.10, it gives appropriate result, but if I use same thing in an if statement, it doesn't!

@jpollard: It worked fine, but is there any difference with use of ";" or without using it? An explaination would be helpful for me. :)

grail 12-14-2012 02:14 PM

Pretty simple really:
Code:

if($3 > 0.1);
The above says, if expression is true, do nothing, ie everything between the last bracket and the semi colon is what is to be executed once the test is true.

MensaWater 12-14-2012 02:15 PM

Quote:

Originally Posted by shivaa (Post 4849337)
@MensaWater: It worked, but why this sort of behaviour? I remember, I had done similar tests earlier, and they were doing exactly what I wanted. For example, if I simply test $3 > 0.10, it gives appropriate result, but if I use same thing in an if statement, it doesn't!

The input can be string, integer or floating point. If you review the awk man page it will tell you that it determines which type you're using but then also notes you can put the 0 in front of the variable to insure it treats it as a numeric (integer or floating point) as opposed to string.

Various programs do this kind of thing and may do it differently but it is always best if there is a way to tell it the type explicitly as there is in awk to go ahead and do it to remove ambiguity. It may be awk saw the "." as string so treated the numbers before and after it as part of the string and with different quoting it would have forced it to see the entire thing as floating point. However since there is the easy method of forcing it to see it as numeric I wouldn't bother looking further but you can feel free to do so.

jpollard 12-14-2012 02:42 PM

From the manpage on awk, Control Statements:
Code:

      if (condition) statement [ else statement ]
A "statement" is either a block statement - starting with "{" and ending with }" or a simple statement, an assignment or function call and terminated with a newline or a ";" in the case of a multi-statement line.

In your case the "if(...);" was one statement, followed by another "print $3"

shivaa 12-14-2012 02:43 PM

Thanks you very much!
Aw for Awk.. Aw for Awesome.. just love it! :)


All times are GMT -5. The time now is 11:57 AM.