Information Technology Reference
In-Depth Information
variable containing the number of fields in the current input record. Thus, one
can loop over all fields in the current input record using the for -statement of awk
and manipulate every field separately. Alternatively, $(1) $(9) can be addressed
as $1 $9 . The symbols/strings $0 and $(0) stand for the entire pattern space.
Example: The following program firstFiveFieldsPerLine prints the first five
fields in every line separated by one blank. It can be used to isolate starting phrases
of sentences, if a text file is formatted in such a way that every line contains an
entire single sentence. For example, it enables an educator to check whether his
or her students use transition signals such as “ First ”, “ Next ”, “ In short ”or“ In
conclusion ” in their writing.
#!/bin/sh
# firstFiveFieldsPerLine
awk '{ print $1 , $2 , $3 , $4 , $5 }' $1
Recall that the trailing $1 represents the input file name for the Bourne shell. The
commas trigger printing of the built-in variable OFS (output field separator) which
is set to a blank by default.
Built-In Operators and Functions
awk has built-in operators for numerical computation, Boolean or logical opera-
tions, string manipulation, pattern matching and assignment of values to variables.
The following lists all awk operators in decreasing order of precedence, i.e. , operators
on top of this list are applied before operators that are listed subsequently, if the
order of execution is not explicitly set by parentheses.
Note that strings other than those that have the format of numbers all have the
value 0 in numerical computations.
Increment operators ++ , -- . Comment: ++var increments the variable var by 1
before it is used. var++ increments var by 1 immediately after it was used (in that
particular spot of the expression and the program).
Algebraic operators * , / , % . Comment: Multiplication, division, and integer division
remainder (mod-operator).
Concatenation of strings. Nothing or white space ( cf. Section 12.4.1.5).
Relational operators for comparison > , >= , < , <= , == , != , ~ , !~ . Comment: == , !=
stand for “equal” and “not equal,” respectively. ~ , !~ stand for “matches pattern”
and “does not match pattern,” respectively. For example, x~/a/ is satisfied, if the
string in variable x contains the letter a . If it is not clear what sort of comparison
is meant, then awk uses string comparison instead of numerical comparison.
! . Logical not.
&& . Logical and.
|| . Logical or.
Assignment operators = , += , -= , *= , /= and %= . Comment: = is the assignment
operator that assigns a value to a variable. The other assignment operators += , -= ,
*= , /= and %= exist just for notational convenience as, e.g. , in C [31]. For example,
var+=d sets var to var+d . This the same as var=var+d .
In addition to the above operators, the following built-in functions can be used
in awk programs:
int , sqrt , exp , log . Comment: int( expression ) is the integer part of expression .
sqrt() is the square root function. exp() is the exponential function to base e and
log() is its inverse.
Search WWH ::




Custom Search