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 Expressions

Expressions are the basic building blocks of any programming language. They form its core logic. Expressions evaluate to a value that we can test, print, or pass to any function. They are also used to assign a new value to a variable. AWK expressions are made up of operators and operands, which consist of constants, variables, regular expressions, and function calls.

In this chapter, we will cover the following topics:

  • AWK variables and constants
  • Expressions using binary arithmetic operators
  • Expressions using assignment operations
  • Expressions using increment and decrement operators
  • Expressions using relational operators
  • Expressions using logical operators
  • Expressions using ternary operators
  • Unary arithmetic expressions
  • Exponential expressions
  • String concatenation
  • Regular expression operators

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...

Arithmetic expressions using binary operators

AWK supports almost all basic arithmetic operators for building arithmetic expressions. These operators are binary operators; that is, they operate on two variables and are very similar to C language expressions. All these arithmetic operators follow the normal precedence rule. AWK supports the following arithmetic operators.

Addition (p + q):

This is represented by a plus (+) symbol, which adds two or more numbers. These numbers could be variables or constants. For example, we can add two numbers after assigning them to two variables as follows:

$ awk 'BEGIN{ p = 20; q = 30; print "( p + q ) = ",( p + q )}'

The output on execution of this code is as follows:

( p + q ) =  50

Now, we will use the marks.txt sample file to calculate the sum of marks obtained by a student using the arithmetic operator, as follows:

...

Assignment expressions

An assignment is an expression that stores a value in a variable. The simplest assignment operator is =, the equals sign. It stores the value of the right-hand-side operand as such. The assignment statement syntax is as follows:

<variable> or <field> or <array> = <constant> or <expression> or

The basic variable assignment is represented by the equals sign, =. Whatever value was assigned earlier is forgotten and the new value is assigned. For example, we assign a value to variable x=10 as follows:

$ awk 'BEGIN{ x=10; print "Number x is : ", x}'

The output on execution of the preceding code is as follows:

Number x is :  10

Assignment can store string values as well. For example, now we declare a variable message and store the string Welcome to Awk Programming. To store this string, we use two more variables...

Increment and decrement expressions

AWK also supports the increment ++ and decrement -- operators; they increase or decrease the value of a variable by one. Both operators are similar to the operators in C. These operators can only be used with a single variable and, thus, only before or after the variable. They are the short forms of some common operations of adding and subtracting.

Pre-increment:

It is represented by the plus plus (++) symbol prefixed to the variable. It increments the value of an operand by one. Let's say we have a variable, var; to pre-increment its value, we write ++var. It first increments the value of operand and then returns the incremented value. In the following example, we use two variables, p =5 and q = ++p. Here, first the value is incremented and then it is assigned. So, the pre-increment sets both the operands p and q to 6. It is equivalent...

Relational expressions

Relational expressions are built using relational operators, also known as conditional or comparison operators. These operators are used to test conditions, like if or while. Relational expressions compare strings or numbers for relationships such as equality, greater than , less than, and so on.

These expressions return 1 if the condition evaluates to true and 0 if false. When comparing operands of different types, numeric operands are converted to strings using a built-in variable (using CONVFMT). Strings are compared character to character.

Equal to (==):

This relational operator is represented by the equals symbol repeated twice (==). It returns true if both operands are equal; otherwise it returns false. Here, we have to make sure not to mistype the == operator by forgetting to put one = character. In this case, the AWK code still remains valid but...

Logical or Boolean expressions

Boolean expressions are also known as logical expressions. It is a combination of comparison expressions or matching expressions, using the Boolean operators together with parentheses to control nesting. There are three Boolean operators, namely or ( ||) , and (&&), and not (!). The truth value of Boolean expression is calculated by combining the truth values of the component expressions.

Boolean expressions are used to combine two or more conditional expressions. They return numeric value 1 if true and 0 if false.

Logical AND (&&): This operator is represented by the && symbol. Its syntax is:

expr1 && expr2

It evaluates to true if both expressions expr1 and expr2 evaluate to true; otherwise it returns false. Also expr2 is evaluated if and only if expr1 evaluates to true. For example, we print those records from the...

Ternary expressions

Ternary expressions are also known as conditional expressions. They are a special kind of expression that has three operands. In this expression, we use one expression's value to select one of two other expressions. It works the same way as in C language. Its syntax is as follows:

conditional exp1 ? Statement 1 : statement 2

If conditional expression exp1 returns true, then Statement1 gets executed; otherwise, statement2 gets executed. For example, here we use two variables and find largest number from two given numbers as follows:

$ vi ternary.awk

BEGIN {
p = 10; q=20
( p > q )? max=p: max =q
print max
}

$ awk -f ternary.awk

The output on execution of the preceding code is as follows:

20 

In the following example, we prefix each line with a number, but we only print the numbers if the line is not blank. We use the NF built-in variable...

Unary expressions

An operator that accepts a single operand is called a unary operator, and expressions built using unary operator are called unary expressions. Increment and decrement operators also fall under this category.

Unary plus:

It is represented by a single plus (+) symbol. It multiplies a single operand by +1. In the following example, we assign a variable p with value -5. On applying the unary plus operator to the variable, it multiplies the variable p with +1 and again stores the result inside p, as follows:

$ awk 'BEGIN{ p = -5; p = +p; print "p = ",p }' 

The output on execution of the preceding code is as follows:

p =  -5 

Unary minus:

It is represented by a single minus (-) symbol. It multiplies a single operand by -1. In the following example, we assign a variable p with value -5. On applying the unary minus operator, it multiplies the variable...

Exponential expressions

There are two formats of exponential operators:

Exponential format 1 (^):

This is an exponential operator that raises the value of an operand. For example, the following example raises the value of 5 by the power of 3:

$ awk 'BEGIN { a = 5; a = a ^ 3; print "a ^ 3 =",a }'

The output on execution of the preceding code is as follows:

a ^ 3 = 125 

Exponential format 2 (**):

This also raises the value of an operand. For example, the following example raises the value of 5 by the power of 3:

$ awk 'BEGIN { a = 5; a = a ** 3; print "a ** 3 =",a }'

The output on execution of the preceding code is as follows:

a ** 3 = 125

String concatenation

There is no specific operation to represent string concatenation. Space is a string concatenation operator that is used to merge two strings.

In the following example, we create three string variables to perform concatenation at different locations. In the statement, str3 contains the concatenated value of str1 and str2. Each print statement performs string concatenation with a static string value and AWK variable:

$ vi string.awk

BEGIN {
OFS=","
str1 = "Good"
str2 = "Morning"
num1 = "10"
str3 = str1 " " str2;
print "Concatenated string is : " str3
num1 = num1+1
print "string to number conversion on addition : " num1
}

$ awk -f string.awk

The output on execution of the preceding code is as follows :

Concatenated string is : Good Morning
string to number conversion...

Regular expression operators

When we use the == condition, AWK looks for an exact match. However, when we use the match operator (~), AWK looks for a partial match. Here, ~ means contains. To match a specific pattern using regular expression, ~ and !~ are used. Regular expression comparisons are performed using a matching expression built with either of these two operators. The right-hand side of the ~ or !~ operator could be a regular expression or string enclosed between forward slashes (/…/).

Match operator (~):

It is represented as a tilde (~) symbol. It matches a pattern in a specific field. Its syntax is as follows:

expression ~ /regexpr/

It matches if the string value of the expression contains a sub-string matched by regular expression regexpr.

For example, if you want to print the records containing Singh or Kapur in last name field from the employees database...

Operators' Precedence

It determines how operators are grouped when different operators appear in a single expression. For example, * has higher precedence than +. So, if we have a + b * c ;it means multiply b and c and then add a to the result. Precedence of operators can be overridden by using parentheses.

This is table of AWK's operator precedence order from highest to lowest:

...

Operator

Description

(…)

Grouping

$

Field reference

++ or - -

Increment, decrement

^ or **

Exponentiation

+, -, !

Unary plus, minus, logical not

*, /, %

Multiplication, division or remainder

+, -

Addition, subtraction

Space

String concatenation

&lt; &lt;= == != > >= >>

Relational operators

~ !~

Match and no match operator

in

Array membership

&&

Logical and

||

Logical or

Summary

In this chapter we learned about different types elements of computation, that is, expressions in AWK. We learned how they are made up of different types of constants and variables. Then we learned about building different types of expressions using unary, binary, assignment, arithmetic, and Relational operators. Then we covered the ternary operator and string concatenation. Finally, we understood regular expression operators and their usage, followed by operator precedence.

In the next chapter, we will learn about the usage of different control flow statements in AWK programs.

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