1. Overview

Awk is a versatile and powerful text-processing language supporting many string and arithmetic functions.

In this tutorial, we’ll learn how to take the absolute value of a number in Awk.

2. Understanding the Scenario

Let’s start by taking a look at the sample input.txt file:

$ cat input.txt
-100
900
-2
-4.5

We can see that the file contains some negative values.

Most programming languages support an abs() function to get the absolute value of numbers. Let’s see if there’s an in-built abs() function in Awk for our use case:

$ awk '{
    print abs($1);
}' input.txt
awk: cmd. line:1: (FILENAME=input.txt FNR=1) fatal: function `abs' not defined

Unfortunately, we got an error suggesting that the abs() function isn’t available. So, we need to find other ways to solve our use case.

3. Using Conditional Operator

Awk supports the conditional operator (?) to choose between two expressions based on a condition:

condition ? expression1 : expression2

Let’s use the conditional operator to get the absolute value of the first field ($1) from the input.txt file:

$ awk '{
    abs_val = ($1 >= 0) ? $1 : -$1; 
    print abs_val;
}' input.txt
100
900
2
4.5

Great! We got the absolute value in the abs_val variable, and the result looks correct.

4. Using the if Condition

Alternatively, we can use an if condition to negate the negative values and get the absolute value. Let’s see this in action:

$ awk '{
    abs_val = $1;
    if (abs_val < 0) { 
        abs_val = -$1;
    }
    print abs_val;
}' input.txt
100
900
2
4.5

It works as expected!

5. Using sqrt() Function

Mathematically speaking, we can get the absolute value of a number, N, using the square (^2) and square root operators:

√N^2 = |N|

Within the Awk community of developers, this is a common technique to get absolute values.

Let’s use this approach to solve our use case of getting the absolute value of the first field ($1) from the input.txt file:

$ awk '{
    abs_val = sqrt($1^2); 
    print abs_val;
}' input.txt
100
900
2
4.5

Perfect! We got this one right!

6. Using gsub() Function

Firstly, we should acknowledge that Awk is designed and optimized for performing text-processing tasks. So, taking a text-oriented approach to solve our use case is natural.

Now, one possible approach for calculating the absolute value of numbers is to replace the negative sign () with an empty string. For this purpose, we can use the gsub() function to replace a pattern in a string:

gsub(regexp, replacement, target)

Lastly, let’s use gsub() to replace the negative sign () with an empty string for solving our use case:

$ awk '{
    abs_val = $1;
    gsub(/-/, "", abs_val); 
    print abs_val;
}' input.txt
100
900
2
4.5

Our approach worked as expected.

7. Using User-Defined Function

We’ve learned many approaches to getting absolute values. However, the most developer-centric approach is to write a custom function to get the absolute value because we don’t get it directly from Awk.

So, let’s define the my_abs() function using the conditional operator approach:

function my_abs(x) { 
    return x < 0 ? -x : x 
}

We must note that we could also use other approaches within the body of the my_abs() function.

Next, let’s see the complete awk script in action, where we call the my_abs() function to get the absolute value of the first field ($1):

$ awk '
function my_abs(x) { 
    return x < 0 ? -x : x 
}

{
    abs_val = my_abs($1); 
    print abs_val;
}' input.txt
100
900
2
4.5

Fantastic! It looks like we nailed this one.

8. Conclusion

In this article, we learned how to get the absolute value of numbers in Awk. Furthermore, while solving the use case, we explored the sqrt() and gsub() functions along with the if condition, user-defined function, and conditional operator (?).

Comments are closed on this article!