- 论坛徽章:
- 23
|
回复 #20 ghostgorst 的帖子
man awk
AWK PROGRAM EXECUTION
An AWK program consists of a sequence of pattern-action statements and
optional function definitions.
pattern { action statements }
function name(parameter list) { statements }
Gawk first reads the program source from the program-file(s) if speci-
fied, from arguments to --source, or from the first non-option argument
on the command line. The -f and --source options may be used multiple
times on the command line. Gawk reads the program text as if all the
program-files and command line source texts had been concatenated
together. This is useful for building libraries of AWK functions,
without having to include them in each new AWK program that uses them.
It also provides the ability to mix library functions with command line
programs.
The environment variable AWKPATH specifies a search path to use when
finding source files named with the -f option. If this variable does
not exist, the default path is ".:/usr/local/share/awk". (The actual
directory may vary, depending upon how gawk was built and installed.)
If a file name given to the -f option contains a "/" character, no path
search is performed.
Gawk executes AWK programs in the following order. First, all variable
assignments specified via the -v option are performed. Next, gawk com-
piles the program into an internal form. Then, gawk executes the code
in the BEGIN block(s) (if any), and then proceeds to read each file
named in the ARGV array. If there are no files named on the command
line, gawk reads the standard input.
If a filename on the command line has the form var=val it is treated as
a variable assignment. The variable var will be assigned the value
val. (This happens after any BEGIN block(s) have been run.) Command
line variable assignment is most useful for dynamically assigning val-
ues to the variables AWK uses to control how input is broken into
fields and records. It is also useful for controlling state if multi-
ple passes are needed over a single data file.
If the value of a particular element of ARGV is empty (""), gawk skips
over it.
For each record in the input, gawk tests to see if it matches any pat-
tern in the AWK program. For each pattern that the record matches, the
associated action is executed. The patterns are tested in the order
they occur in the program.
Finally, after all the input is exhausted, gawk executes the code in
the END block(s) (if any). |
|