⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 awk.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>awk</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="awk"></a> <a name="tag_04_06"></a><!-- awk --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright &copy; 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_04_06_01"></a>NAME</h4><blockquote>awk - pattern scanning and processing language</blockquote><h4><a name="tag_04_06_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>awk</tt> <b>[</b><tt>-F</tt> <i>ERE</i><b>][</b><tt>-v</tt> <i>assignment</i><b>]</b> <tt>...</tt> <i>program</i><b>[</b><i>argument</i> <tt>...</tt><b>]</b><tt><br><br>awk</tt> <b>[</b><tt>-F</tt> <i>ERE</i><b>]</b> <tt>-f</tt> <i>progfile</i> <tt>...</tt> <b>[</b><tt>-v</tt><i>assignment</i><b>]</b> <tt>...</tt><b>[</b><i>argument</i> <tt>...</tt><b>]</b><tt><br></tt></code></p></blockquote><h4><a name="tag_04_06_03"></a>DESCRIPTION</h4><blockquote><p>The <i>awk</i> utility shall execute programs written in the <i>awk</i> programming language, which is specialized for textualdata manipulation. An <i>awk</i> program is a sequence of patterns and corresponding actions. When input is read that matches apattern, the action associated with that pattern is carried out.</p><p>Input shall be interpreted as a sequence of records. By default, a record is a line, less its terminating &lt;newline&gt;, butthis can be changed by using the <b>RS</b> built-in variable. Each record of input shall be matched in turn against each pattern inthe program. For each pattern matched, the associated action shall be executed.</p><p>The <i>awk</i> utility shall interpret each input record as a sequence of fields where, by default, a field is a string of non-&lt;blank&gt;s. This default white-space field delimiter can be changed by using the <b>FS</b> built-in variable or <b>-F</b><i>ERE</i>. The <i>awk</i> utility shall denote the first field in a record $1, the second $2, and so on. The symbol $0 shall referto the entire record; setting any other field causes the re-evaluation of $0. Assigning to $0 shall reset the values of all otherfields and the <b>NF</b> built-in variable.</p></blockquote><h4><a name="tag_04_06_04"></a>OPTIONS</h4><blockquote><p>The <i>awk</i> utility shall conform to the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax Guidelines</a>.</p><p>The following options shall be supported:</p><dl compact><dt><b>-F&nbsp;</b> <i>ERE</i></dt><dd>Define the input field separator to be the extended regular expression <i>ERE</i>, before any input is read; see <a href="#tag_04_06_13_04">Regular Expressions</a> .</dd><dt><b>-f&nbsp;</b> <i>progfile</i></dt><dd>Specify the pathname of the file <i>progfile</i> containing an <i>awk</i> program. If multiple instances of this option arespecified, the concatenation of the files specified as <i>progfile</i> in the order specified shall be the <i>awk</i> program. The<i>awk</i> program can alternatively be specified in the command line as a single argument.</dd><dt><b>-v&nbsp;</b> <i>assignment</i></dt><dd>The application shall ensure that the <i>assignment</i> argument is in the same form as an <i>assignment</i> operand. The specifiedvariable assignment shall occur prior to executing the <i>awk</i> program, including the actions associated with <b>BEGIN</b>patterns (if any). Multiple occurrences of this option can be specified.</dd></dl></blockquote><h4><a name="tag_04_06_05"></a>OPERANDS</h4><blockquote><p>The following operands shall be supported:</p><dl compact><dt><i>program</i></dt><dd>If no <b>-f</b> option is specified, the first operand to <i>awk</i> shall be the text of the <i>awk</i> program. Theapplication shall supply the <i>program</i> operand as a single argument to <i>awk</i>. If the text does not end in a&lt;newline&gt;, <i>awk</i> shall interpret the text as if it did.</dd><dt><i>argument</i></dt><dd>Either of the following two types of <i>argument</i> can be intermixed: <dl compact><dt><i>file</i></dt><dd>A pathname of a file that contains the input to be read, which is matched against the set of patterns in the program. If no<i>file</i> operands are specified, or if a <i>file</i> operand is <tt>'-'</tt> , the standard input shall be used.</dd><dt><i>assignment</i></dt><dd>An operand that begins with an underscore or alphabetic character from the portable character set (see the table in the BaseDefinitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap06.html#tag_06_01">Section 6.1, PortableCharacter Set</a>), followed by a sequence of underscores, digits, and alphabetics from the portable character set, followed by the<tt>'='</tt> character, shall specify a variable assignment rather than a pathname. The characters before the <tt>'='</tt>represent the name of an <i>awk</i> variable; if that name is an <i>awk</i> reserved word (see <a href="#tag_04_06_13_16">Grammar</a> ) the behavior is undefined. The characters following the equal sign shall be interpreted as if theyappeared in the <i>awk</i> program preceded and followed by a double-quote ( <tt>' )'</tt> character, as a <b>STRING</b> token (see<a href="#tag_04_06_13_16">Grammar</a> ), except that if the last character is an unescaped backslash, it shall be interpreted as aliteral backslash rather than as the first character of the sequence <tt>"\""</tt> . The variable shall be assigned the value ofthat <b>STRING</b> token and, if appropriate, shall be considered a <i>numeric string</i> (see <a href="#tag_04_06_13_02">Expressions in awk</a> ), the variable shall also be assigned its numeric value. Each such variable assignmentshall occur just prior to the processing of the following <i>file</i>, if any. Thus, an assignment before the first <i>file</i>argument shall be executed after the <b>BEGIN</b> actions (if any), while an assignment after the last <i>file</i> argument shalloccur before the <b>END</b> actions (if any). If there are no <i>file</i> arguments, assignments shall be executed beforeprocessing the standard input.</dd></dl></dd></dl></blockquote><h4><a name="tag_04_06_06"></a>STDIN</h4><blockquote><p>The standard input shall be used only if no <i>file</i> operands are specified, or if a <i>file</i> operand is <tt>'-'</tt> ;see the INPUT FILES section. If the <i>awk</i> program contains no actions and no patterns, but is otherwise a valid <i>awk</i>program, standard input and any <i>file</i> operands shall not be read and <i>awk</i> shall exit with a return status of zero.</p></blockquote><h4><a name="tag_04_06_07"></a>INPUT FILES</h4><blockquote><p>Input files to the <i>awk</i> program from any of the following sources shall be text files:</p><ul><li><p>Any <i>file</i> operands or their equivalents, achieved by modifying the <i>awk</i> variables <b>ARGV</b> and <b>ARGC</b></p></li><li><p>Standard input in the absence of any <i>file</i> operands</p></li><li><p>Arguments to the <b>getline</b> function</p></li></ul><p>Whether the variable <b>RS</b> is set to a value other than a &lt;newline&gt; or not, for these files, implementations shallsupport records terminated with the specified separator up to {LINE_MAX} bytes and may support longer records.</p><p>If <b>-f</b> <i>progfile</i> is specified, the application shall ensure that the files named by each of the <i>progfile</i>option-arguments are text files and their concatenation, in the same order as they appear in the arguments, is an <i>awk</i>program.</p></blockquote><h4><a name="tag_04_06_08"></a>ENVIRONMENT VARIABLES</h4><blockquote><p>The following environment variables shall affect the execution of <i>awk</i>:</p><dl compact><dt><i>LANG</i></dt><dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume ofIEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> forthe precedence of internationalization variables used to determine the values of locale categories.)</dd><dt><i>LC_ALL</i></dt><dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd><dt><i>LC_COLLATE</i></dt><dd>Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regularexpressions and in comparisons of string values.</dd><dt><i>LC_CTYPE</i></dt><dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte asopposed to multi-byte characters in arguments and input files), the behavior of character classes within regular expressions, theidentification of characters as letters, and the mapping of uppercase and lowercase characters for the <b>toupper</b> and<b>tolower</b> functions.</dd><dt><i>LC_MESSAGES</i></dt><dd>Determine the locale that should be used to affect the format and contents of diagnostic messages written to standarderror.</dd><dt><i>LC_NUMERIC</i></dt><dd>Determine the radix character used when interpreting numeric input, performing conversions between numeric and string values, andformatting numeric output. Regardless of locale, the period character (the decimal-point character of the POSIX locale) is thedecimal-point character recognized in processing <i>awk</i> programs (including assignments in command line arguments).</dd><dt><i>NLSPATH</i></dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt><i>PATH</i></dt><dd>Determine the search path when looking for commands executed by <i>system</i>(<i>expr</i>), or input and output pipes; see theBase Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap08.html">Chapter 8, EnvironmentVariables</a>.</dd></dl><p>In addition, all environment variables shall be visible via the <i>awk</i> variable <b>ENVIRON</b>.</p></blockquote><h4><a name="tag_04_06_09"></a>ASYNCHRONOUS EVENTS</h4><blockquote><p>Default.</p></blockquote><h4><a name="tag_04_06_10"></a>STDOUT</h4><blockquote><p>The nature of the output files depends on the <i>awk</i> program.</p></blockquote><h4><a name="tag_04_06_11"></a>STDERR</h4><blockquote><p>The standard error shall be used only for diagnostic messages.</p></blockquote><h4><a name="tag_04_06_12"></a>OUTPUT FILES</h4><blockquote><p>The nature of the output files depends on the <i>awk</i> program.</p></blockquote><h4><a name="tag_04_06_13"></a>EXTENDED DESCRIPTION</h4><blockquote><h5><a name="tag_04_06_13_01"></a>Overall Program Structure</h5><p>An <i>awk</i> program is composed of pairs of the form:</p><pre><i>pattern</i> <tt>{</tt> <i>action</i> <tt>}</tt></pre><p>Either the pattern or the action (including the enclosing brace characters) can be omitted.</p><p>A missing pattern shall match any record of input, and a missing action shall be equivalent to:</p><pre><tt>{ print }</tt></pre><p>Execution of the <i>awk</i> program shall start by first executing the actions associated with all <b>BEGIN</b> patterns in theorder they occur in the program. Then each <i>file</i> operand (or standard input if no files were specified) shall be processed inturn by reading data from the file until a record separator is seen ( &lt;newline&gt; by default). Before the first reference to afield in the record is evaluated, the record shall be split into fields, according to the rules in <a href="#tag_04_06_13_04">Regular Expressions</a> , using the value of <b>FS</b> that was current at the time the record was read. Eachpattern in the program then shall be evaluated in the order of occurrence, and the action associated with each pattern that matchesthe current record executed. The action for a matching pattern shall be executed before evaluating subsequent patterns. Finally,the actions associated with all <b>END</b> patterns shall be executed in the order they occur in the program.</p><h5><a name="tag_04_06_13_02"></a>Expressions in awk</h5><p>Expressions describe computations used in <i>patterns</i> and <i>actions</i>. In the following table, valid expressionoperations are given in groups from highest precedence first to lowest precedence last, with equal-precedence operators groupedbetween horizontal lines. In expression evaluation, where the grammar is formally ambiguous, higher precedence operators shall beevaluated before lower precedence operators. In this table <i>expr</i>, <i>expr1</i>, <i>expr2</i>, and <i>expr3</i> represent anyexpression, while lvalue represents any entity that can be assigned to (that is, on the left side of an assignment operator). Theprecise syntax of expressions is given in <a href="#tag_04_06_13_16">Grammar</a> .</p><center><a name="tagtcjh_10"></a><b>Table: Expressions in Decreasing Precedence in <i>awk</i></b></center><center><table border="1" cellpadding="3" align="center"><tr valign="top"><th align="center"><p class="tent"><b>Syntax</b></p></th><th align="center"><p class="tent"><b>Name</b></p></th><th align="center"><p class="tent"><b>Type of Result</b></p></th><th align="center"><p class="tent"><b>Associativity</b></p></th></tr><tr valign="top"><td align="left"><p class="tent">( <i>expr</i> )</p></td><td align="left"><p class="tent">Grouping</p></td><td align="left"><p class="tent">Type of <i>expr</i></p></td><td align="left"><p class="tent">N/A</p></td></tr><tr valign="top"><td align="left"><p class="tent">$<i>expr</i></p></td><td align="left"><p class="tent">Field reference</p></td><td align="left"><p class="tent">String</p></td><td align="left"><p class="tent">N/A</p></td></tr><tr valign="top"><td align="left"><p class="tent">++ lvalue</p></td>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -