Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-02-2009, 06:17 AM   #1
Registered: Aug 2008
Posts: 54

Rep: Reputation: 15
Absolute Value function in AWK

I was wondering if there was an absolute value function in awk? More specifically, I need it in a string of boolean operators.

Right now I have:

awk '{ if ( $1 == 0 && $2 != $1 && [absolute value of $1-$2] <= 6 ) print }'

But I don't know what I need to put in the brackets.
Old 11-02-2009, 06:31 AM   #2
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
I figured it out, but in case anyone was interested or has a similar problem, the line is:

awk '{ if ( $1 == 0 && $2 != $1 && ( $1-$2 >=0 ? $1=$2 : $2-$1 ) <= 6 ) print }'
Old 11-02-2009, 06:50 AM   #3
Registered: Jan 2009
Location: Dhahran, Saudi Arabia
Distribution: RHEL 5
Posts: 42

Rep: Reputation: 17
Ummm one could implement an absoluteness function by simply:

function abs(value)
if(value < 0)
return value * -1
return value

didn't test it but it should work :P
Old 11-02-2009, 07:05 AM   #4
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,690

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
That might be simplified to:
function abs(value)
  return (value<0?-value:value);


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
Is there a function similar to awk for C++? binarybob0001 Programming 12 06-16-2011 04:43 PM
How to use variables in search pattern in gensub function of awk rajeshksv Linux - Newbie 1 08-07-2009 07:07 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
How to use split function with AWK command? intikhabalam Linux - General 4 07-29-2008 10:40 AM
Need help with awk function philosophia Programming 1 02-20-2007 05:53 PM

All times are GMT -5. The time now is 09:37 AM.

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