LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-14-2012, 09:49 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
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.
 
Old 12-14-2012, 09:54 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 1,982

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

Without the ";" in there.
 
1 members found this post helpful.
Old 12-14-2012, 10:00 AM   #3
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,937
Blog Entries: 5

Rep: Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752
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.
 
1 members found this post helpful.
Old 12-14-2012, 10:20 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
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.
 
Old 12-14-2012, 01:14 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
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.
 
1 members found this post helpful.
Old 12-14-2012, 01:15 PM   #6
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,937
Blog Entries: 5

Rep: Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752
Quote:
Originally Posted by shivaa View Post
@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.
 
1 members found this post helpful.
Old 12-14-2012, 01:42 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 1,982

Rep: Reputation: 512Reputation: 512Reputation: 512Reputation: 512Reputation: 512Reputation: 512
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"
 
1 members found this post helpful.
Old 12-14-2012, 01:43 PM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Original Poster
Rep: Reputation: 285Reputation: 285Reputation: 285
Thanks you very much!
Aw for Awk.. Aw for Awesome.. just love it!
 
1 members found this post helpful.
  


Reply

Tags
awk


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] problem if/else statement with awk beca1234567 Linux - Newbie 3 12-13-2012 07:47 PM
[SOLVED] awk else if statement dazdaz Programming 2 05-19-2012 05:56 AM
[SOLVED] Getting -nan in output of awk statement sparker1970 Linux - Newbie 3 04-06-2012 10:45 AM
[SOLVED] variable substitution in awk statement emmalg Linux - Software 12 07-02-2009 08:39 AM
how to keep Field Separator in AWK when using a sub statement tmcguinness Programming 4 02-09-2009 02:24 PM


All times are GMT -5. The time now is 05:34 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration