LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Absolute Value function in AWK (https://www.linuxquestions.org/questions/linux-newbie-8/absolute-value-function-in-awk-766196/)

bioinformatics_guy 11-02-2009 06:17 AM

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.

bioinformatics_guy 11-02-2009 06:31 AM

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 }'

geek.ksa 11-02-2009 06:50 AM

Ummm one could implement an absoluteness function by simply:

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

didn't test it :) but it should work :P

jlliagre 11-02-2009 07:05 AM

That might be simplified to:
Code:

function abs(value)
{
  return (value<0?-value:value);
}



All times are GMT -5. The time now is 02:39 PM.