Absolute value | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 05-11-2005
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 28 March 2015, 10:08 AM EDT
Location: Foxborough, MA
Posts: 7,764
Thanks: 170
Thanked 633 Times in 591 Posts
echo '-12' | nawk '{ print ($1 >= 0) ? $1 : 0 - $1}'
Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 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 Unix and Linux 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 Unix and Linux 12-04-2007
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 28 March 2015, 10:08 AM EDT
Location: Foxborough, MA
Posts: 7,764
Thanks: 170
Thanked 633 Times in 591 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 08:01 PM
absolute value dr46014 UNIX for Dummies Questions & Answers 3 08-26-2008 07:48 AM
get absolute filename mainegate Shell Programming and Scripting 0 08-21-2007 12:59 PM
absolute path filedeliver Programming 4 06-05-2007 05:18 PM
Getting absolute value ksh icyhot UNIX for Dummies Questions & Answers 3 03-30-2006 05:05 PM



All times are GMT -4. The time now is 06:38 AM.