Jump to content

An Awk Primer/Numbers and Strings

From Wikibooks, open books for an open world

Numbers

[edit | edit source]

Numbers can be expressed in Awk as either decimal integers or floating-point quantities. For example:

  • 789
  • 3.141592654
  • +67
  • +4.6E3
  • -34
  • -2.1e-2

There is no provision for specifying values in other bases, such as hex or octal; however, as will be shown later, it is possible to output them from Awk in hex or octal format.

Strings

[edit | edit source]

Strings are expressed in double quotes. For example:

  • "All work and no play makes Jack a homicidal maniac!"
  • "1987A1"
  • "do re mi fa so la ti do"

Awk also supports null strings, which are represented by empty quotes: "".

Like the C programming language, it is possible in Awk to specify a character by its three-digit octal code (preceded by a backslash).

There are various "special" characters that can be embedded into strings:

  • \n Newline (line feed)
  • \t Horizontal tab (aligns to next column of 8 spaces)
  • \b Backspace
  • \r Carriage return
  • \f Form feed (possibly causes a new page, if printing)

A double-quote (") can be embedded in a string by preceding it with a backslash, and a backslash can be embedded in a string by typing it in twice: \\. If a backslash is used with other characters (say, \m), it is simply treated as a normal character.