Built-in functions

In the previous example, we defined a function called green. This leads into taking about some built-in functions that come with awk.

AWK comes with many built-in functions such as mathematical functions:

  • sin(x)
  • cos(x)
  • sqrt(x)
  • exp(x)
  • log(x)
  • rand()

You can use them like this:

$ awk 'BEGIN{x=sqrt(5); print x}' 

Also, there are built-in functions that can be used in string manipulation:

$ awk 'BEGIN{x = "welcome"; print toupper(x)}'

Get Mastering Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.