Reader small image

You're reading from  Learning AWK Programming

Product typeBook
Published inMar 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788391030
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Shiwang Kalkhanda
Shiwang Kalkhanda
author image
Shiwang Kalkhanda

Shiwang Kalkhanda (RHCA, RHCSS, MCSE) is a Linux geek and consultant with expertise in the automation of infrastructure deployment and management. He has more than 10 years' experience in security, system, and network administration, and training on open source tech. For most of his automation work, he uses Shell Scripting, Python, and Go. He holds a master's and a bachelor's degree in computer applications. He enjoys traveling and spending time with his children. He is also the author of a book on text processing utilities in Unix-like environments, Learning Awk Programming.
Read more about Shiwang Kalkhanda

Right arrow

AWK Functions

A function, also known as a named procedure, is a set of instructions that is used by programming languages to return a single result or a set of results. The statement that requests the function is called a function call. The functions extend the usefulness and functionality of AWK. This chapter covers the different types of built-in functions that are available in AWK. The built-in functions of AWK are generally divided into three categories, namely numeric, string, and I/O. Apart from these, we will cover the additional functions provided by GAWK to represent time, to provide type information, and to enable bit manipulation. Then, we will discuss how AWK is used for writing user-defined functions for use in the rest of the program.

In this chapter, we will cover the following topics:

  • Arithmetic functions
  • String functions
  • Input/output functions
  • Time functions
  • ...

Built-in functions

Built-in functions are always available to the programmer for use in the program. This section covers the built-in functions in AWK. These functions generally accept arguments as input and return a value. Whitespace is ignored between the built-in function name and the opening parenthesis; however, we should avoid using whitespace in this way as user-defined functions do not permit whitespace.

If an expression is given as an argument to the function, the expression is evaluated before the call is made to the function. For example:

In the preceding case, p is incremented to the value of 6 before the sqrt function is called. It is good practice to evaluate the expression first and then pass the argument to the function.

Arithmetic functions

...

User-defined functions

AWK allows us to define user-defined functions. A large complex can be divided into functions where each function performs a specific task. These functions can be written and tested independently. This functionality means that we can reuse code.

Function definition and syntax

The definition of functions can be given anywhere between the rules of an AWK program. It is not mandatory in AWK to define a function before calling it because AWK first reads the entire program before it starts to execute it. The general syntax for defining a user-defined function is as follows:

function function_name(argument1, argument2, …local variable.) {
body-of-function
...

Summary

In this chapter, we learned about different types of built-in and user-defined functions that accept zero or more arguments and return a value. Function arguments can be made up of expressions that are evaluated before calling a function. We began by looking at built-in functions such as arithmetic functions, which are used for numeric processing. We followed this up by looking at string functions that are used for string manipulations and for matching the occurrences of a pattern in a string. Then, we looked at various input/output functions, such as the close() function for closing files and pipes. After this, we looked at the time functions, which can be quite useful when it comes to timestamping or creating log files. We followed this by bit-manipulation functions, which perform bitwise operations on two or more integers. Finally, we looked at how to define and call...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning AWK Programming
Published in: Mar 2018Publisher: PacktISBN-13: 9781788391030
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Shiwang Kalkhanda

Shiwang Kalkhanda (RHCA, RHCSS, MCSE) is a Linux geek and consultant with expertise in the automation of infrastructure deployment and management. He has more than 10 years' experience in security, system, and network administration, and training on open source tech. For most of his automation work, he uses Shell Scripting, Python, and Go. He holds a master's and a bachelor's degree in computer applications. He enjoys traveling and spending time with his children. He is also the author of a book on text processing utilities in Unix-like environments, Learning Awk Programming.
Read more about Shiwang Kalkhanda