LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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,800

Original Poster
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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,800

Original Poster
Blog Entries: 4

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


Reply

Tags
awk



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:43 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration