home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
gnu
/
gawk-2.15.5-src.lha
/
gawk-2.15.5
/
gawk.info-5
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-06-12
|
49KB
|
959 lines
This is Info file gawk.info, produced by Makeinfo-1.55 from the input
file /gnu/src/amiga/gawk-2.15.5/gawk.texi.
This file documents `awk', a program that you can use to select
particular records in a file and perform operations upon them.
This is Edition 0.15 of `The GAWK Manual',
for the 2.15 version of the GNU implementation
of AWK.
Copyright (C) 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided that
the entire resulting derived work is distributed under the terms of a
permission notice identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that this permission notice may be stated in a
translation approved by the Foundation.
File: gawk.info, Node: For Statement, Next: Break Statement, Prev: Do Statement, Up: Statements
The `for' Statement
===================
The `for' statement makes it more convenient to count iterations of a
loop. The general form of the `for' statement looks like this:
for (INITIALIZATION; CONDITION; INCREMENT)
BODY
This statement starts by executing INITIALIZATION. Then, as long as
CONDITION is true, it repeatedly executes BODY and then INCREMENT.
Typically INITIALIZATION sets a variable to either zero or one,
INCREMENT adds 1 to it, and CONDITION compares it against the desired
number of iterations.
Here is an example of a `for' statement:
awk '{ for (i = 1; i <= 3; i++)
print $i
}'
This prints the first three fields of each input record, one field per
line.
In the `for' statement, BODY stands for any statement, but
INITIALIZATION, CONDITION and INCREMENT are just expressions. You
cannot set more than one variable in the INITIALIZATION part unless you
use a multiple assignment statement such as `x = y = 0', which is
possible only if all the initial values are equal. (But you can
initialize additional variables by writing their assignments as
separate statements preceding the `for' loop.)
The same is true of the INCREMENT part; to increment additional
variables, you must write separate statements at the end of the loop.
The C compound expression, using C's comma operator, would be useful in
this context, but it is not supported in `awk'.
Most often, INCREMENT is an increment expression, as in the example
above. But this is not required; it can be any expression whatever.
For example, this statement prints all the powers of 2 between 1 and
for (i = 1; i <= 100; i *= 2)
print i
Any of the three expressions in the parentheses following the `for'
may be omitted if there is nothing to be done there. Thus,
`for (;x > 0;)' is equivalent to `while (x > 0)'. If the CONDITION is
omitted, it is treated as TRUE, effectively yielding an "infinite loop"
(i.e., a loop that will never terminate).
In most cases, a `for' loop is an abbreviation for a `while' loop,
as shown here:
INITIALIZATION
while (CONDITION) {
BODY
INCREMENT
}
The only exception is when the `continue' statement (*note The
`continue' Statement: Continue Statement.) is used inside the loop;
changing a `for' statement to a `while' statement in this way can
change the effect of the `continue' statement inside the loop.
There is an alternate version of the `for' loop, for iterating over
all the indices of an array:
for (i in array)
DO SOMETHING WITH array[i]
*Note Arrays in `awk': Arrays, for more information on this version of
the `for' loop.
The `awk' language has a `for' statement in addition to a `while'
statement because often a `for' loop is both less work to type and more
natural to think of. Counting the number of iterations is very common
in loops. It can be easier to think of this counting as part of
looping rather than as something to do inside the loop.
The next section has more complicated examples of `for' loops.
File: gawk.info, Node: Break Statement, Next: Continue Statement, Prev: For Statement, Up: Statements
The `break' Statement
=====================
The `break' statement jumps out of the innermost `for', `while', or
`do'-`while' loop that encloses it. The following example finds the
smallest divisor of any integer, and also identifies prime numbers:
awk '# find smallest divisor of num
{ num = $1
for (div = 2; div*div <= num; div++)
if (num % div == 0)
break
if (num % div == 0)
printf "Smallest divisor of %d is %d\n", num, div
else
printf "%d is prime\n", num }'
When the remainder is zero in the first `if' statement, `awk'
immediately "breaks out" of the containing `for' loop. This means that
`awk' proceeds immediately to the statement following the loop and
continues processing. (This is very different from the `exit'
statement which stops the entire `awk' program. *Note The `exit'
Statement: Exit Statement.)
Here is another program equivalent to the previous one. It
illustrates how the CONDITION of a `for' or `while' could just as well
be replaced with a `break' inside an `if':
awk '# find smallest divisor of num
{ num = $1
for (div = 2; ; div++) {
if (num % div == 0) {
printf "Smallest divisor of %d is %d\n", num, div
break
}
if (div*div > num) {
printf "%d is prime\n", num
break
}
}
}'
File: gawk.info, Node: Continue Statement, Next: Next Statement, Prev: Break Statement, Up: Statements
The `continue' Statement
========================
The `continue' statement, like `break', is used only inside `for',
`while', and `do'-`while' loops. It skips over the rest of the loop
body, causing the next cycle around the loop to begin immediately.
Contrast this with `break', which jumps out of the loop altogether.
Here is an example:
# print names that don't contain the string "ignore"
# first, save the text of each line
{ names[NR] = $0 }
# print what we're interested in
END {
for (x in names) {
if (names[x] ~ /ignore/)
continue
print names[x]
}
}
If one of the input records contains the string `ignore', this
example skips the print statement for that record, and continues back to
the first statement in the loop.
This is not a practical example of `continue', since it would be
just as easy to write the loop like this:
for (x in names)
if (names[x] !~ /ignore/)
print names[x]
The `continue' statement in a `for' loop directs `awk' to skip the
rest of the body of the loop, and resume execution with the
increment-expression of the `for' statement. The following program
illustrates this fact:
awk 'BEGIN {
for (x = 0; x <= 20; x++) {
if (x == 5)
continue
printf ("%d ", x)
}
print ""
}'
This program prints all the numbers from 0 to 20, except for 5, for
which the `printf' is skipped. Since the increment `x++' is not
skipped, `x' does not remain stuck at 5. Contrast the `for' loop above
with the `while' loop:
awk 'BEGIN {
x = 0
while (x <= 20) {
if (x == 5)
continue
printf ("%d ", x)
x++
}
print ""
}'
This program loops forever once `x' gets to 5.
As described above, the `continue' statement has no meaning when
used outside the body of a loop. However, although it was never
documented, historical implementations of `awk' have treated the
`continue' statement outside of a loop as if it were a `next' statement
(*note The `next' Statement: Next Statement.). By default, `gawk'
silently s