Information Technology Reference
In-Depth Information
Note that the output of
context
is, essentially, eleven times the size of the
input for the example just listed. It may be advisable to incorporate any desired,
subsequent pattern matching for the strings that are printed by
context
into an
extended version of this program.
Control Structures
awk
has two special control structures
next
and
exit
. In addition,
awk
has the
usual control structures:
if
,
for
and
while
.
next
is a statement that starts processing the next input line immediately from
the top of the
awk
program.
next
is the analogue of the
d
operator in
sed
.
exit
is
a statement that causes
awk
to terminate immediately.
exit
is the analogue of the
q
operator in
sed
.
The
if
statement looks the same as in C [31, p. 55]:
if (
conditional
){
action1
}
else {
action2
}
conditional
can be any of the types of conditionals we defined above for address
patterns including Boolean combinations of comparison of algebraic expressions in-
cluding the use of variables. If a regular expression
/
regExpr
/
is intended to match
or not to match the entire pattern space
$0
in
conditional
, then this has to be de-
noted explicitely using the match-operator
~
. Thus, one has to use
$0~/
regExpr
/
or
$0!~/
regExpr
/
respectively. The
else
part of the
if
statement can be omitted or
can follow on the same line.
Example:
The use of a
for
-statement in connection with an
if
-statement is
shown in the next example. We shall refer to the following program as
findFourLetterWords
. It shows a typical use of
for
and
if
,
i.e.
, looping over all
fields with
for
, and on condition determined by
if
taking some action on the fields.
1: #!/bin/sh
2: # findFourLetterWords (awk version)
3: leaveOnlyWords $1
|
4: awk '
{ for(f=1;f<=NF;f++) {
5:
if($(f)!~/^[A-Za-z][a-z][a-z][a-z]$/) { $(f)="" }
6:
}
7:
}
8:
/[^ ]/ { print NR , $0 }
9:
' -
|
adjustBlankTabs -
Explanation:
The
for
-loop in line 4 processes every field (addressed as
$(f)
in
line 5,
f
the counter variable) from left to right. If a field
$(f)
does not match the
pattern
/^[A-Za-z][a-z][a-z][a-z]$/
, then it is set to the empty string in line 5.
In case the pattern space stays non-white (
/[^ ]/
) after this procedure, it is printed
in line 8 with a leading line-number
NR
. Finally, blanks are properly adjusted in the
output by
adjustBlankTabs
.
The technique how to loop over associative arrays has already been demonstrated
in the listing of the program
countFrequencies
in Section 12.2.2.
Similar to the
for
statement, the
while
statement also looks the same as in
C
[31, p. 60]:
while (
conditional
){
action
}
.
Search WWH ::
Custom Search