AWK variables and constants

This sections describes the different types of AWK variables and constants available in the AWK programming language.

AWK variables give names to values for use or reference later in another part of the program. AWK variables are case sensitive. The AWK variable name should begin with an alphabet and the rest of the characters can be numbers, letters, or underscore. AWK keywords cannot be used as variable names. Variables are assigned new values using assignment operators, increment operators, and decrement operators. AWK also has some built-in variables, which have special meaning; however, they can be used and assigned like other variables. All built-in variables of AWK are named in uppercase.

Inside AWK, we don't have to declare a variable to use it. Also, there is no need to initialize an AWK variable explicitly. Variables are automatically initialized to empty strings; if the variable is a number, it is initialized to zero upon conversion. And if we wish to initialize an AWK variable, then the best place to do it is in the BEGIN section, which is executed only once.

An AWK constant is the simplest type of expression, which always has the same value. There are three types of constants, namely string, numeric, and regexp constants. Numeric constants represent numbers; this can be an integer, a decimal fraction, or an exponential notation. In AWK, all numbers are in decimal representation (base 10) by default. String constants represent a sequence of characters enclosed in double quotes. Strings in AWK can be of any length and they can have any of the possible 8-bit ASCII characters. A regular expression constant is a regex expression enclosed in forward slashes. Most regular expressions in AWK are built using constants but sometimes they  can be built using expressions.

Now we will study operators that make use of the values provided by constants and variables in building expressions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.17.40