AWK tutorial: awk custom function and awk example

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)}'
awk custom function

OK.

Related post

awk command tutorial in linux/unix with examples and use cases
Awk tutorial: awk merge files base on the keyword
Awk tutorial: find and kill process use awk
Awk tutorial: awk -F and awk BEGIN{ FS … }
Awk tutorial: awk regex example
Awk tutorial: awk loop example: awk for and while
awk if else, if else if else and awk if else nested example
Awk tutorial: awk NR FNR difference
Awk tutorial: awk sort uniq
Awk tutorial: split on character awk or cut
Awk tutorial: awk newline character
Awk tutorial: three ways of awk execution
How to print tree directory structure in linux/unix
Linux shell batch move files, delete spaces, rename files

Add a Comment

Your email address will not be published. Required fields are marked *