Site icon LinuxCommands.site

awk custom function and examples

Linux awk command can help us with some numerical calculations, we sometimes use absolute values.

So how does awk achieve the absolute value of the number?

The mathematical absolute value symbol “|x|” cannot be used in awk.

Here, you need to use the awk custom function to implement the abs function.

awk abs example

➜  ~ echo -5 | awk 'function abs(x){return (x < 0) ? -x : x;} {print abs($0)}'

OK.

Exit mobile version