📄 0162-0164.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Complete Command Reference:User Commands:EarthWeb Inc.-</TITLE>
</HEAD>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311046 //-->
<!-- TITLE=Linux Complete Command Reference//-->
<!-- AUTHOR=Red Hat//-->
<!-- PUBLISHER=Macmillan Computer Publishing//-->
<!-- IMPRINT=Sams//-->
<!-- CHAPTER=01 //-->
<!-- PAGES=0001-0736 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0159-0161.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0165-0166.html">Next</A></CENTER></P>
<A NAME="PAGENUM-162"><P>Page 162</P></A>
<TABLE>
<TR><TD>
_W compat, __compat
</TD><TD>
Run in compatibility mode. In compatibility mode,
gawk behaves identically to awk; none of the GNU-specific extensions are recognized. See "GNU Extensions," later in this
manual page, for more information.
</TD></TR><TR><TD>
_W copyleft, _W copyright,
__copyleft, __copyright
</TD><TD>
Print the short version of the GNU copyright information message on the error output.
</TD></TR><TR><TD>
_W help, _W usage
</TD><TD>
Print a relatively short summary of the available options on the error output. Per the GNU
</TD></TR><TR><TD>
__help, __usage
</TD><TD>
Coding Standards, these options cause an immediate, successful exit.
</TD></TR><TR><TD>
_W lint, __lint
</TD><TD>
Provide warnings about constructs that are dubious or nonportable to other
awk implementations.
</TD></TR><TR><TD>
_W posix, __posix
</TD><TD>
This turns on
compatibility mode, with the following additional
restrictions:\x escape sequences are not recognized.
</TD></TR><TR><TD>
</TD><TD>
The synonym func for the keyword function is not recognized.
</TD></TR><TR><TD>
</TD><TD>
The operators ** and **= cannot be used in place of
^ and ^=.
</TD></TR><TR><TD>
_W source=program-text,
</TD><TD>
Use program-text as
awk program source code. This option allows the easy intermixing of
</TD></TR><TR><TD>
__source=program-text
</TD><TD>
library functions (used via the
_f and __file options) with source code entered on
the command line. It is intended primarily for medium to large
awk programs used in shell scripts.
</TD></TR><TR><TD>
</TD><TD>
The _W source= form of this option uses the rest of the command-line argument
for program-text; no other options to _W will be recognized in the same argument.
</TD></TR><TR><TD>
_W version, __version
</TD><TD>
Print version information for this particular copy of
gawk on the error output. This is useful mainly for knowing if the current copy of
gawk on your system is up-to-date with respect to whatever the Free Software Foundation is distributing. Per the GNU Coding
Standards, these options cause an immediate, successful exit.
</TD></TR><TR><TD>
__
</TD><TD>
Signal the end of options. This is useful to allow further arguments to the
awk program itself to start with a _. This is mainly for consistency with the argument-parsing convention
used by most other programs.
</TD></TR></TABLE>
<P>In compatibility mode, any other options are flagged as illegal, but are otherwise ignored. In normal operation, as long
as program text has been supplied, unknown options are passed on to the
awk program in the ARGV array for processing. This
is particularly useful for running awk programs via the
#! executable interpreter mechanism.
</P>
<P><B>
AWK PROGRAM EXECUTION
</B></P>
<P>An awk program consists of a sequence of
pattern-action statements and optional function definitions:
</P>
<!-- CODE SNIP //-->
<PRE>
pattern { action statements }
function name(parameter list) { statements }
</PRE>
<!-- END CODE SNIP //-->
<P>gawk first reads the program source from the program-file(s) if specified, from arguments to
_W source=, or from the first nonoption argument on the command line. The
_f and _W source= options may be used multiple times on the
command line. gawk will read 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.
</P>
<P>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/lib/awk:/usr/local/lib/awk.
</P>
<P>If a filename given to the _f option contains a
/ character, no path search is performed.
</P>
<P>gawk executes awk programs in the following order. First, all variable assignments specified via the
_v option are performed. Next, gawk compiles 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.
</P>
<P>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
</P>
<A NAME="PAGENUM-163"><P>Page 163</P></A>
<P>useful for dynamically assigning values to the variables
awk uses to control how input is broken into fields and records. It
is also useful for controlling state if multiple passes are needed over a single data file.
</P>
<P>If the value of a particular element of ARGV is empty
(""), gawk skips over it.
</P>
<P>For each line in the input, gawk tests to see if it matches any
pattern in the awk program. For each pattern that the
line matches, the associated action is executed. The patterns are tested in the order they occur in the program.
</P>
<P>Finally, after all the input is exhausted, gawk
executes the code in the END block(s) (if any).
</P>
<P><B>
VARIABLES AND FIELDS
</B></P>
<P>awk variables are dynamic; they come into existence when they are first used. Their values are either floating-point
numbers or strings, or both, depending upon how they are used.
awk also has one-dimensional arrays; arrays with multiple
dimensions may be simulated. Several predefined variables are set as a program runs; these will be described as needed and
summarized in the "Built-In Variables" subsection.
</P>
<P><B>
FIELDS
</B></P>
<P>As each input line is read, gawk splits the line into fields, using the value of the
FS variable as the field separator. If FS is a single character, fields are separated by that character. Otherwise,
FS is expected to be a full regular expression. In the
special case that FS is a single blank, fields are separated by runs of blanks or tabs. Note that the value of
IGNORECASE (see the following) will also affect how fields are split when
FS is a regular expression.
</P>
<P>If the FIELDWIDTHS variable is set to a space separated list of numbers, each field is expected to have fixed width, and
gawk will split up the record using the specified widths. The value of
FS is ignored. Assigning a new value to FS overrides the use
of FIELDWIDTHS, and restores the default behavior.
</P>
<P>Each field in the input line may be referenced by its position,
$1, $2, and so on. $0 is the whole line. The value of a field
may be assigned to as well. Fields need not be referenced by constants:
</P>
<!-- CODE SNIP //-->
<PRE>
n =5
print $n
</PRE>
<!-- END CODE SNIP //-->
<P>prints the fifth field in the input line. The variable
NF is set to the total number of fields in the input line.
</P>
<P>References to nonexistent fields (that is, fields after
$NF) produce the null-string. However, assigning to a nonexistent
field (for example, $(NF+2) = 5) will increase the value of
NF, create any intervening fields with the null string as their value,
and cause the value of $0 to be recomputed, with the fields being separated by the value of
OFS. References to negative-numbered fields cause a fatal error.
</P>
<P><B>
BUILT-IN VARIABLES
</B></P>
<P>awk's built-in variables are the following:
</P>
<TABLE>
<TR><TD>
ARGC
</TD><TD>
The number of command-line arguments (does not include options to
gawk, or the program source).
</TD></TR><TR><TD>
ARGIND
</TD><TD>
The index in ARGV of the current file being processed.
</TD></TR><TR><TD>
ARGV
</TD><TD>
Array of command-line arguments. The array is indexed from
0 to ARGC _ 1. Dynamically changing the contents of
ARGV can control the files used for data.
</TD></TR><TR><TD>
CONVFMT
</TD><TD>
The conversion format for numbers,
"%.6g", by default.
</TD></TR><TR><TD>
ENVIRON
</TD><TD>
An array containing the values of the current environment. The array is indexed by the
environment variables, each element being the value of that variable (for example,
ENVIRON["HOME"] might be /u/arnold). Changing this array does not affect the environment seen by programs which
gawk spawns via redirection or the system() function. (This may change in a future version of
gawk.)
</TD></TR><TR><TD>
ERRNO
</TD><TD>
If a system error occurs either doing a redirection for
getline, during a read for getline, or during a
close(), then ERRNO will contain a string describing the error.
</TD></TR><TR><TD>
FIELDWIDTHS
</TD><TD>
A whitespace-separated list of fieldwidths. When set,
gawk parses the input into fields of fixed width, instead of using the value of the
FS variable as the field separator. The fixed field width facility is
still experimental; expect the semantics to change as
gawk evolves over time.
</TD></TR></TABLE>
<A NAME="PAGENUM-164"><P>Page 164</P></A>
<TABLE>
<TR><TD>
FILENAME
</TD><TD>
The name of the current input file. If no files are specified on the command line, the value of
FILENAME is _. However, FILENAME is undefined inside the
BEGIN block.
</TD></TR><TR><TD>
FNR
</TD><TD>
The input record number in the current input file.
</TD></TR><TR><TD>
FS
</TD><TD>
The input field separator, a blank by default.
</TD></TR><TR><TD>
IGNORECASE
</TD><TD>
Controls the case-sensitivity of all regular expression operations. If
IGNORECASE has a nonzero value, then pattern matching in rules, field splitting with
FS, regular expression matching with ~ and !~, and
the gsub(), index(), match(), split(),and sub() predefined functions will all ignore case when doing
regular expression operations. Thus, if IGNORECASE is not equal to zero,
/aB/ matches all of the strings ab, aB, Ab, and AB. As with all
awk variables, the initial value of IGNORECASE is zero, so all regular expression
operations are normally case-sensitive.
</TD></TR><TR><TD>
NF
</TD><TD>
The number of fields in the current input record.
</TD></TR><TR><TD>
NR
</TD><TD>
The total number of input records seen so far.
</TD></TR><TR><TD>
OFMT
</TD><TD>
The output format for numbers,
"%.6g", by default.
</TD></TR><TR><TD>
OFS
</TD><TD>
The output field separator, a blank by default.
</TD></TR><TR><TD>
ORS
</TD><TD>
The output record separator, by default a newline.
</TD></TR><TR><TD>
RS
</TD><TD>
The input record separator, by default a newline.
RS is exceptional in that only the first character of
its string value is used for separating records. (This will probably change in a future release of
gawk.) If RS is set to the null string, then records are separated by blank lines. When
RS is set to the null string, then the newline character always acts as a field separator, in addition to whatever value
FS may have.
</TD></TR><TR><TD>
RSTART
</TD><TD>
The index of the first character matched by
match(); 0 if no match.
</TD></TR><TR><TD>
RLENGTH
</TD><TD>
The length of the string matched by
match(); _1 if no match.
</TD></TR><TR><TD>
SUBSEP
</TD><TD>
The character used to separate multiple subscripts in array elements, by default
n034.
</TD></TR></TABLE>
<P><B>
ARRAYS
</B></P>
<P>Arrays are subscripted with an expression between square brackets. If the expression is an expression list
(expr, expr ...) then the array subscript is a string consisting of the concatenation of the (string) value of each expression, separated by
the value of the SUBSEP variable. This facility is used to simulate multiply dimensioned arrays. For
example,
</P>
<!-- CODE SNIP //-->
<PRE>
i = "A" ; j = "B" ; k = "C"
x[i, j, k] = "hello, world\n"
</PRE>
<!-- END CODE SNIP //-->
<P>assigns the string hello, world\n to the element of the array
x which is indexed by the string
"A\034B\034C". All arrays in awk are associative, that is indexed by string values.
</P>
<P>The special operator in may be used in an if or
while statement to see if an array has an index consisting of a
particular value:
</P>
<!-- CODE SNIP //-->
<PRE>
if (val in array)
print array[val]
</PRE>
<!-- END CODE SNIP //-->
<P>If the array has multiple subscripts, use (i,j)in
array.
</P>
<P>The in construct may also be used in a for loop to iterate over all the elements of an array.
</P>
<P>An element may be deleted from an array using the
delete statement. The delete statement may also be used to delete
the entire contents of an array.
</P>
<P><B>
VARIABLE TYPING AND CONVERSION
</B></P>
<P>Variables and fields may be floating-point numbers, or strings, or both. How the value of a variable is interpreted
depends upon its context. If used in a numeric expression, it will be treated as a number; if used as a string, it will be treated as
a string.
</P>
<P>To force a variable to be treated as a number, add
0 to it; to force it to be treated as a string, concatenate it with the
null string.
</P>
<P><CENTER>
<a href="0159-0161.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0165-0166.html">Next</A></CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -