Absolute value | Unix Linux Forums | Shell Programming and Scripting

 Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

# Absolute value

## Shell Programming and Scripting

#1
05-11-2005
 placroix1 Registered User Join Date: May 2005 Last Activity: 5 October 2005, 10:59 AM EDT Posts: 7 Thanks: 0 Thanked 0 Times in 0 Posts
Absolute value

Is there a function in awk to get the absolute value of a-b which can be negative or positive, I just care for the absolute value...
#2
05-11-2005
 vgersh99   Forum Advisor Join Date: Feb 2005 Last Activity: 19 April 2014, 2:05 AM EDT Location: Foxborough, MA Posts: 7,526 Thanks: 137 Thanked 534 Times in 502 Posts
echo '-12' | nawk '{ print (\$1 >= 0) ? \$1 : 0 - \$1}'
#3
05-11-2005
 placroix1 Registered User Join Date: May 2005 Last Activity: 5 October 2005, 10:59 AM EDT Posts: 7 Thanks: 0 Thanked 0 Times in 0 Posts
Thanks 80)
#4
10-05-2005
 LouPelagalli Registered User Join Date: May 2005 Last Activity: 31 October 2005, 4:14 PM EST Posts: 11 Thanks: 0 Thanked 0 Times in 0 Posts
Thanks vgersh99, it works for me too!
#5
12-04-2007
 ahjiefreak Registered User Join Date: Nov 2007 Last Activity: 11 December 2008, 9:39 AM EST Posts: 132 Thanks: 0 Thanked 0 Times in 0 Posts
Hi vgersh99,

I saw this post and I wonder if I have an associative array for calculate an operation; how abs would work in this case.

Assume;

I have
count[\$1]=\$2-\$1;

If I apply the one you suggested in earlier post,

can it be turned to;

count[\$1]= (\$4-\$5)*(0.05);
if(count[\$1]>=0){
count[\$1]=count[\$1];}
else
count[\$1]=0-count[\$1];

-Jason
#6
12-04-2007
 vgersh99   Forum Advisor Join Date: Feb 2005 Last Activity: 19 April 2014, 2:05 AM EDT Location: Foxborough, MA Posts: 7,526 Thanks: 137 Thanked 534 Times in 502 Posts
Quote:
 Originally Posted by ahjiefreak Hi vgersh99, I saw this post and I wonder if I have an associative array for calculate an operation; how abs would work in this case.
sure, why not!
Quote:
 Originally Posted by ahjiefreak Assume; I have count[\$1]=\$2-\$1; If I apply the one you suggested in earlier post, can it be turned to; count[\$1]= (\$4-\$5)*(0.05); if(count[\$1]>=0){ count[\$1]=count[\$1];} else count[\$1]=0-count[\$1];

Code:
```   count[\$1]= (\$4-\$5)*(0.05)
if(count[\$1]<0) count[\$1]=0-count[\$1]```

Quote:
 Originally Posted by ahjiefreak Please advise. Really hope to hear from you soon! Thanks -Jason