LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices



Reply
 
Search this Thread
Old 04-07-2008, 08:34 AM   #1
jkeertir
Member
 
Registered: Mar 2008
Posts: 70

Rep: Reputation: 15
awk query


Dear all,

I want to know how can i compare the value of third field with value 0 using awk.

for ex i print vale of $3 using awk ,i get
1] 2] 2] ........
i want to change if i can compare and see if any its value is 0;

With Regards,
Keerti
 
Old 04-07-2008, 08:58 AM   #2
livetoday
Member
 
Registered: Jun 2006
Location: India
Distribution: RHEL,Suse,Fedora
Posts: 106

Rep: Reputation: 15
Quote:
awk '$3 ~ 0 {print}' filename
will show the line only if 3rd field is zero...
 
Old 04-07-2008, 09:04 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by livetoday View Post
will show the line only if 3rd field is zero...
yes but only is its really a 0 at 3rd column. If 3rd column is for example "test0test" it will match. "~" is matching operator for regular expression. the correct operator to use is "==" for equality.
 
Old 04-07-2008, 09:07 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
If you get a numeric value followed by "]" you can strip out the bracket and use the numerical value for comparison by the function gensub, as in
Code:
if ( gensub("]","","g",$3) == 0 )
   print "number is zero"
else
   print "number is not zero"
Note that the gensub function does not alter the value of $3, unlike the functions sub and gsub.
 
  


Reply


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 07:23 AM
interesting MySQL query/view query :s mjh Programming 3 03-25-2008 08:30 AM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 06:39 PM
awk program. a little query! indiancosmonaut Programming 7 07-05-2007 10:48 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 07:36 PM


All times are GMT -5. The time now is 01:14 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration