LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   awk question (http://www.linuxquestions.org/questions/programming-9/awk-question-647123/)

vikas027 06-05-2008 02:03 AM

awk question
 
Hi all,

I am new to awk programming. Going through awk tutorials, I came across this example.

Code:

sh-3.00# cat emp
Chen Cho        5/19/63 203-344-1234    $76
Tom Billy      4/12/45 913-972-4536    $102
Larry White    11/2/54 908-657-2389    $54
Bill Clinton    1/14/60 654-576-4114    $201
Steve Ann      9/15/71 202-545-8899    $58

sh-3.00# awk '$4 < 40' emp
Chen Cho        5/19/63 203-344-1234    $76
Steve Ann      9/15/71 202-545-8899    $58


I am not able to understand, how $4 which is
Code:

203-344-1234
913-972-4536
908-657-2389
654-576-4114
202-545-8899

is being compared to 40. :confused:

Pls elaborate.

Thanks in adv.
VIKAS

colucix 06-05-2008 02:24 AM

The question is: what do you expect by comparing a string with an integer? According to the "GAWK: Effective AWK Programming" guide a comparison between a string and a number follows the rules of string comparison and
Quote:

Strings are compared by comparing the first character of each, then the second character
of each, and so on.
Looking at the first two characters of the string in the 4th field, the result of the comparison is
Code:

203-344-1234 < 40 --> TRUE
913-972-4536 < 40 --> FALSE
908-657-2389 < 40 --> FALSE
654-576-4114 < 40 --> FALSE
202-545-8899 < 40 --> TRUE

but not in a numeric sense. Indeed in this case only the first character serves to determine the result, since 2 is less than 4, 9 is greater than 4 and so on... in the ASCII table order.

vikas027 06-05-2008 03:18 AM

Many many Thanks colucix.


All times are GMT -5. The time now is 02:53 AM.