Absolute value | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-11-2005
placroix1 placroix1 is offline
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...
Sponsored Links
    #2  
Old 05-11-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 19 December 2014, 11:15 PM EST
Location: Foxborough, MA
Posts: 7,679
Thanks: 156
Thanked 591 Times in 555 Posts
echo '-12' | nawk '{ print ($1 >= 0) ? $1 : 0 - $1}'
Sponsored Links
    #3  
Old 05-11-2005
placroix1 placroix1 is offline
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  
Old 10-05-2005
LouPelagalli LouPelagalli is offline
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!
Sponsored Links
    #5  
Old 12-04-2007
ahjiefreak ahjiefreak is offline
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];


Please advise. Really hope to hear from you soon! Thanks

-Jason
Sponsored Links
    #6  
Old 12-04-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 19 December 2014, 11:15 PM EST
Location: Foxborough, MA
Posts: 7,679
Thanks: 156
Thanked 591 Times in 555 Posts
Quote:
Originally Posted by ahjiefreak View Post
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
absolute sum onesuri Shell Programming and Scripting 5 05-20-2011 09:01 PM
absolute value dr46014 UNIX for Dummies Questions & Answers 3 08-26-2008 08:48 AM
get absolute filename mainegate Shell Programming and Scripting 0 08-21-2007 01:59 PM
absolute path filedeliver Programming 4 06-05-2007 06:18 PM
Getting absolute value ksh icyhot UNIX for Dummies Questions & Answers 3 03-30-2006 06:05 PM



All times are GMT -4. The time now is 01:46 AM.