Jump to content

An Awk Primer/Operations

From Wikibooks, open books for an open world

Relational Operators

[edit | edit source]

Awk's relational operations have already been discussed. As a quick reminder, here they are:

  • < Less than
  • <= Less than or equal to
  • > Greater than
  • >= Greater than or equal to
  • == Equal to
  • != Not equal to
  • ~ Matches (compares a string to a regular expression)
  • !~ Does not match

Note that, unlike some languages, relational expressions in Awk do not return a value. They only evaluate to a true condition or a false condition. That means that a Awk program like this:

BEGIN {a=1; print (a==1)}

It doesn't print anything at all, and trying to use relational expressions as part of an arithmetic expression causes an error.

Logic Operators

[edit | edit source]

To group together the relational operator into more complex expressions, Awk provides three logic (or Boolean) operators:

  • && And (reports "true" if both sides are true)
  • || Or (reports "true" if either side, or both, are true)
  • ! Not (Reverses true/false of the following expression)

Arithmetic Operators

[edit | edit source]

Awk uses the standard four arithmetic operators:

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • ^ Exponentiation (** may also work)
  • % Remainder

All computations are performed in floating-point. They are performed with the expected order of operations.

Increments

[edit | edit source]

There are increment and decrement operators:

  • ++ Increment
  • -- Decrement

The position of these operators with respect to the variable they operate on is important. If ++ precedes a variable, that variable is incremented before it is used in some other operation. For example:

BEGIN {x=3; print ++x} 

This will print 4. If ++ follows a variable, that variable is incremented after it is used in some other operation. For example:

BEGIN {x=3; print x++}

This will print 3, but x will equal four from that point on. Similar remarks apply to --. Of course, if the variable being incremented or decremented is not part of some other operation at that time, it makes no difference where the operator is placed.

Compound Assignments

[edit | edit source]

Awk also allows the following shorthand operations for modifying the value of a variable:

x += 2
x = x + 2
x -= 2
x = x - 2

You get the idea. This shortcut is available for all of the arithmetic operations (+= -= *= /= ^= %=).

Concatenation

[edit | edit source]

There is only one unique string operation: concatenation. Two strings can be easily concatenated by placing them consecutively on the same line. Only a space needs to separate them. For example:

BEGIN {string = "Super" "power"; print string}

This prints:

Superpower

The strings can be concatenated even if they are variables. This produces the same result as above:

BEGIN {a = "Super"; b = "power"; print (a b)}

The parentheses might not be necessary, but they are often used to make sure that the concatenation is interpreted correctly.

The Conditional

[edit | edit source]

There is an interesting operator called the conditional operator. It has two parts. Look at this example:

print ( price > 500 ? "too expensive" : "cheap" )

This will print either "too expensive" or "cheap" depending on the value of price. The condition before the question mark is evaluated. If true, the first statement is executed, and if false the second is executed. The statements are separated by a colon.