Tutorial Four

Using Logical Operators

In tutorial three you learnt how to filter records by the use of matching operators. We can use logical operators to combine several these conditions.

To select a record from the results file that satisfies more than one condition awk uses the symbols && (double ampersand) as the and operator.

For example, if we have list of second-hand car prices like this...

ford	 mondeo	 1990	5800
ford	 fiesta	 1991	4575
honda	 accord	 1991	6000
toyota	 tercel	 1992	6500
vaxhaull astra	 1990	5950
vaxhaull carlton 1991   6450
...we might want to select all the cars which were made after or during 1991 (column 3) and cost less than 6,250 pounds (column 4). We can do this with the following awk code:

The opposite to the and operator is the or operator. To select cars made either by ford, or vaxhaull, we use the || (double pipe) symbol.

Press the following link if you want to you can try the programming exercise related to tutorial four.

[Help] [Provide some feedback] [Go to Previous Page]