LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-05-2008, 02:03 AM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian
Posts: 1,266

Rep: Reputation: 99
Question 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.

Pls elaborate.

Thanks in adv.
VIKAS
 
Old 06-05-2008, 02:24 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.

Last edited by colucix; 06-05-2008 at 02:28 AM.
 
Old 06-05-2008, 03:18 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian
Posts: 1,266

Original Poster
Rep: Reputation: 99
Many many Thanks colucix.
 
  


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
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
Awk question blanny Linux - Software 6 05-02-2006 08:03 AM
awk question denalitastic Linux - Newbie 1 06-07-2005 10:42 AM
AWK question paraiso Linux - Newbie 5 05-12-2005 01:37 PM
awk question dilberim82 Programming 2 10-01-2003 07:12 PM


All times are GMT -5. The time now is 09:05 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