| Previous | Table of Contents | Next |
The following example matches any line containing the word Apples.
nawk '/Apples/' fruits
Escape Sequences
The following sequences of characters are referred to as escape sequences. Escape sequences specify a notation for characters that are not easily produced from your keyboard and/or cause problems when displayed to your screen. The nawk and echo commands use escape sequences. Although only nawk uses the escape sequences in regular expressions.
|
|
|
| Sequence | Description |
|---|---|
|
|
|
| \b | Matches a backspace. |
| \f | Matches a form feed. |
| \n | Matches a new-line. |
| \r | Matches a carriage return. |
| \t | Matches a tab. |
| \0ddd | Matches the ASCII character for the given octal ASCII code. |
| \c | Matches the literal character c. Use \ to generate a \. |
|
|
|
Expression Patterns
Expression patterns perform number or string comparisons. Nawk provides eight comparison operators. Six of these operators are relational operators used to perform number or string comparison. The other two are string operators used to perform string comparisons.
String Operators may be used to compare a regular expression pattern to a specific field or the entire input line. The following two operators are supported:
|
|
|
| Operator | Function |
|---|---|
|
|
|
| /RE/ | Matches if the current line contains the specified regular expression. Same as the $0 ~ /RE/ command. |
| expr ~ RE | Matches if the string value of expr contains the regular expression
RE. For example,
nawk '$1 ~ /[Aa]pples/' fruits matches lines where field 1 contains "Apples" or "apples." |
| expr !~ RE | Matches if the string value of expr does not contain the regular
expression expr. For example,
nawk '$3 !~ /[Aa]pples/' fruits matches lines where field 1 does not contain the word "Apples" or "apples." |
|
|
|
Relational Operators Relational operators compare strings and numbers. The following six operators are used to compare values.
|
|
|
| Operator | Function |
|---|---|
|
|
|
| lv > rv | True if lv is greater than rv. For example,
nawk 'NF > 3' fruits prints each line containing more than three fields. |
| lv >= rv | True if lv is greater than or equal to rv. For example,
nawk ' $1 >= "M" ' fruits prints each line where the first field begins with a character that has a higher ASCII code than capital M. Such as N, O, P, Q, etc. |
| lv < rv | True if lv is less than rv. |
| lv <= rv | True if lv is less than or equal to rv. |
| lv == rv | True if lv is equal to rv. For instance,
nawk '$1 == "Oranges" ' fruits prints each line where the first field equals the string "Oranges." |
| lv != rv | True if lv is not equal to rv. |
|
|
|
Compound Patterns
A compound pattern is an expression consisting of multiple expression patterns combined with Boolean operators. The Boolean operators are || (OR), && (AND), ! (NOT), and use of parentheses for grouping the operators. If the result of a compound pattern is true, the current input line is matched and the associated action is executed.
Boolean Operators The Boolean operators can be used to combine regular and relational expressions. The following table describes the Boolean operators.
|
|
|
| Operator | Function |
|---|---|
|
|
|
| ! | Negation. True if expression is NOT true. For example,
nawk '! /Apples/' fruits prints each line that does not contain the string "Apples." |
| || | Logical OR. True if either expression is true. For instance,
nawk '$3 > "3,000,000" || $1 == "FRUIT"' fruits prints the header line and each line with bushel counts of more than "3,000,000." Note 3,000,000 is a string, not a number, because of the commas. |
| && | Logical AND. True if both expressions are true. For example,
nawk '$3 > "3,000,000" && NF == 5' fruits prints the header line and each line containing exactly three fields. |
| ( ) | Perform comparisons enclosed together(for grouping). For instance,
nawk '( $1 == "Apples" || $1 == "Oranges" ) && ( NR == 4 )' fruits prints lines where the first field is either "Apples" or "Oranges" and there are exactly four fields. |
|
|
|
Range Patterns
Range patterns contain two patterns separated by a comma (,). A range pattern matches each line from the occurrence of the first pattern to the occurrence of the second pattern. Both patterns may appear on the same line, thus only the one line is matched. If no input line matches the first pattern, no lines are selected. If no input line matches the second pattern, all lines from the first pattern to the end of the file are selected. For example, the following command selects all of the lines from one that contains Apples to one that contains Cherries.
nawk '/Apples/,/Cherries/' fruits
ACTIONS
The action part of an nawk program performs specified actions when a pattern is matched. If no action is specified the default print action is used. The default is the same as specifying { print }, which is equivalent to { print $0 }.
Comments
The nawk command ignores any characters on a line following a # sign. Thus you can document your nawk programs using the number sign.
Constants
Two types of constants are used by nawk, numeric and string. String constants are created by enclosing a sequence of characters in double or single quote marks. For example,
$1 == "FRUIT"
| Previous | Table of Contents | Next |