Expressions

The use of expressions in which you can store, manipulate, and retrieve data is quite different from anything you can do in sed, yet it is a common feature of most programming languages.

An expression is evaluated and returns a value. An expression consists of any combination of numeric and string constants, variables, operators, functions, and regular expressions. We covered regular expressions in detail in Chapter 2, and they are summarized in Appendix B. Functions will be discussed fully in Chapter 9. In this section, we will look at expressions consisting of constants, variables, and operators.

There are two types of constants: string or numeric (“red” or 1). A string must be quoted in an expression. Strings can make use of the escape sequences listed in Table 7.1.

Table 7.1. Escape Sequences
SequenceDescription
\aAlert character, usually ASCII BEL character
\bBackspace
\fFormfeed
\nNewline
\rCarriage return
\tHorizontal tab
\vVertical tab
\dddCharacter represented as 1 to 3 digit octal value
\xhex Character represented as hexadecimal value[4]
\cAny literal character c (e.g., \” for ")[5]

[4] POSIX does not provide “\x”, but it is commonly available.

[5] Like ANSI C, POSIX leaves purposely undefined what you get when you put a backslash before any character not listed in the table. In most awks, you just get that character.

A variable is an identifier that references a value. To define a variable, you only have to name it and assign it a value. The name can only contain ...

Get sed & awk, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.