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

📄 awk.html

📁 unix 下的C开发手册,还用详细的例程。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>awk</title></head><body bgcolor=white><center><font size=2>The Single UNIX &reg; Specification, Version 2<br>Copyright &copy; 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_000_096">&nbsp;</a>NAME</h4><blockquote>awk - pattern scanning and processing language</blockquote><h4><a name = "tag_000_000_097">&nbsp;</a>SYNOPSIS</h4><blockquote><pre><code>awk <b>[</b>-F <i>ERE</i><b>][</b>-v <i>assignment</i><b>]</b> ... <i>program </i><b>[</b><i>argument</i> ...<b>]</b>awk <b>[</b>-F <i>ERE</i><b>] </b>-v <i>progfile</i><b>]</b> ... <b>[</b>-v <i>assignment</i><b>]</b> ...<b>[</b><i>argument</i> ...<b>]</b></code></pre></blockquote><h4><a name = "tag_000_000_098">&nbsp;</a>DESCRIPTION</h4><blockquote>The<i>awk</i>utility executes programs written in the<i>awk</i>programming language, which is specialised for textual data manipulation.An<i>awk</i>program is a sequence of patternsand corresponding actions.When input is read that matches a pattern,the action associated with that patternwill be carried out.<p>Input is interpreted as a sequence of records.By default, a record is a line, but this can be changed by using the<b>RS</b>built-in variable.Each record of input is matched in turnagainst each pattern in the program.For each pattern matched, the associatedaction will be executed.<p>The<i>awk</i>utility interprets each inputrecord as a sequence of fields where, bydefault, a field is a string ofnon-blankcharacters.This default white-space field delimitercan be changed by using the<b>FS</b>built-in variable or the<b>-F</b>&nbsp;<i>ERE</i>.The<i>awk</i>utility denotes the first field in a record$1,the second$2,and so on.The symbol$0refers to the entire record;setting any other field will cause the reevaluation of$0.Assigning to$0will reset the values of all other fields and the<b>NF</b>built-in variable.</blockquote><h4><a name = "tag_000_000_099">&nbsp;</a>OPTIONS</h4><blockquote>The<i>awk</i>utility supports the <b>XBD</b> specification, <a href="../xbd/utilconv.html#usg"><b>Utility Syntax Guidelines</b>&nbsp;</a> .<p>The following options are supported:<dl compact><dt><b>-F&nbsp;</b><i>ERE</i><dd>Define the input field separator to be the extended regular expression<i>ERE</i>,before any input is read; see<xref href=awkre><a href="#tag_000_000_108_004">Regular Expressions</a></xref>.<dt><b>-f&nbsp;</b><i>progfile</i><dd>Specifies the pathname of the file<i>progfile</i>containing an<i>awk</i>program.If multiple instances of this option are specified,the concatenation of the files specified as<i>progfile</i>in the order specified will be the<i>awk</i>program.The<i>awk</i>program can alternatively be specified in thecommand line as a single argument.<dt><b>-v&nbsp;</b><i>assignment</i><dd>The<i>assignment</i>argument must be in the same form as an<i>assignment</i>operand.The specified variable assignment will 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.</dl></blockquote><h4><a name = "tag_000_000_100">&nbsp;</a>OPERANDS</h4><blockquote>The following operands are supported:<dl compact><dt><i>program</i><dd>If no<b>-f</b>option is specified, the first operand to<i>awk</i>will be the text of the<i>awk</i>program.The application will supply the<i>program</i>operand as a single argument to<i>awk</i>.If the text does not end in anewlinecharacter,<i>awk</i>will interpret the text as if it did.<dt><i>argument</i><dd>Either of the following two types of<i>argument</i>can be intermixed:<dl compact><dt><i>file</i><dd>A pathname of a file thatcontains the input to be read, which is matched against the set ofpatterns in the program.If no<i>file</i>operands are specified,or if a<i>file</i>operand is "-", the standard input will be used.<dt><i>assignment</i><dd>An operand that begins with an underscore or alphabetic character fromthe portable character set (see the table inthe <b>XBD</b> specification, <a href="../xbd/charset.html#tag_001_001"><b>Portable Character Set</b>&nbsp;</a> ),followed by a sequence of underscores, digits and alphabetics from theportable character set, followed by the"="character will specify a variable assignment rather than a pathname.The characters before the"="represent the name of an<i>awk</i>variable;if that name is an<i>awk</i>reserved word (see<xref href=awkgram><a href="#tag_000_000_108_016">Grammar</a></xref>)the behaviour is undefined.The characters following the equal sign will be interpreted as ifthey appeared in the<i>awk</i>program preceded and followed by a double-quote(")character, as a<b>STRING</b>token (see<xref href=awkgram><a href="#tag_000_000_108_016">Grammar</a></xref>),except that if the last character is an unescaped backslash,it will be interpreted as a literal backslash rather than asthe first character of the sequence\".The variable will be assigned the value of that<b>STRING</b>token.If that value is considered a<i>numeric string</i>(see<xref href=awkexpr><a href="#tag_000_000_108_002">Expressions in awk</a></xref>),the variable will also be assigned its numeric value.Each such variable assignment will occur just prior tothe processing of the following<i>file</i>,if any.Thus, an assignment before the first<i>file</i>argument will be executed after the<b>BEGIN</b>actions (if any),while an assignment after the last<i>file</i>argumentwill occur before the<b>END</b>actions (if any).If there are no<i>file</i>arguments, assignments will be executed before processing the standard input.</dl><p></dl></blockquote><h4><a name = "tag_000_000_101">&nbsp;</a>STDIN</h4><blockquote>The standard input will be used only if no<i>file</i>operands are specified,or if a<i>file</i>operand is "-".See the INPUT FILES section.</blockquote><h4><a name = "tag_000_000_102">&nbsp;</a>INPUT FILES</h4><blockquote>Input files to the<i>awk</i>program from any of the following sources:<ul><p><li>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>standard input in the absence of any<i>file</i>operands<p><li>arguments to the<b>getline</b>function<p></ul><p>must be text files.Whether the variable<b>RS</b>is set to a value other than anewline characteror not, for these files,implementations supportrecords terminated with thespecified separator up to{LINE_MAX}bytes and may support longer records.<p>If<b>-f</b>&nbsp;<i>progfile</i>is specified,the files named by each of the<i>progfile</i>option-arguments must be text files containing an<i>awk</i>program.</blockquote><h4><a name = "tag_000_000_103">&nbsp;</a>ENVIRONMENT VARIABLES</h4><blockquote>The following environment variables affect the execution of<i>awk</i>:<dl compact><dt><i>LANG</i><dd>Provide a default value for the internationalisation variablesthat are unset or null.If<i>LANG</i>is unset or null, the corresponding value from theimplementation-dependent default locale will be used.If any of the internationalisation variables contains an invalid setting, theutility will behave as if none of the variables had been defined.<dt><i>LC_ALL</i><dd>If set to a non-empty string value,override the values of all the other internationalisation variables.<dt><i>LC_COLLATE</i><dd>Determine the locale for thebehaviour of ranges, equivalence classesand multi-character collating elementswithin regular expressionsand in comparisons of string values.<dt><i>LC_CTYPE</i><dd>Determine thelocale for the interpretation of sequences of bytes of text data ascharacters (for example, single-versus multi-byte characters in arguments and input files),the behaviour of character classes within regular expressions,the identification of characters as letters,and the mapping of upper- and lower-case characters for the<b>toupper</b>and<b>tolower</b>functions.<dt><i>LC_MESSAGES</i><dd>Determine the locale that should be used to affectthe format and contents of diagnosticmessages written to standard error.<dt><i>LC_NUMERIC</i><dd>Determine the radixcharacter used when interpreting numeric input,performing conversions between numeric andstring values and formatting numeric output.Regardless of locale, the periodcharacter (the decimal-point character of the POSIX locale)is the decimal-point character recognised in processing<i>awk</i>programs (including assignments in command-line arguments).<dt><i>NLSPATH</i><dd>Determine the location of message cataloguesfor the processing of<i>LC_MESSAGES .</i><dt><i>PATH</i><dd>Determine the search path when lookingfor commands executed by<i>system</i>(<i>expr</i>),or input and output pipes.Seethe <b>XBD</b> specification, <a href="../xbd/envvar.html"><b>Environment Variables</b>&nbsp;</a> .</dl><p>In addition, all environment variables will be visible via the<i>awk</i>variable<b>ENVIRON</b>.</blockquote><h4><a name = "tag_000_000_104">&nbsp;</a>ASYNCHRONOUS EVENTS</h4><blockquote>Default.</blockquote><h4><a name = "tag_000_000_105">&nbsp;</a>STDOUT</h4><blockquote>The nature of the output files depends on the<i>awk</i>program.</blockquote><h4><a name = "tag_000_000_106">&nbsp;</a>STDERR</h4><blockquote>Used only for diagnostic messages.</blockquote><h4><a name = "tag_000_000_107">&nbsp;</a>OUTPUT FILES</h4><blockquote>The nature of the output files depends on the<i>awk</i>program.</blockquote><h4><a name = "tag_000_000_108">&nbsp;</a>EXTENDED DESCRIPTION</h4><blockquote><h5><a name = "tag_000_000_108_001">&nbsp;</a>Overall Program Structure</h5>An<i>awk</i>program is composed ofpairs of the form:<pre><code><i>pattern</i> { <i>action</i> }</code></pre>Either the pattern or the action (including the enclosingbrace characters) can be omitted.<p>A missing pattern will match anyrecord of input, and amissing action will be equivalent to an action that writesthe matched record of input to standard output.<p>Execution of the<i>awk</i>program starts by first executing the actions associated with all<b>BEGIN</b>patterns in the order they occur in the program.Then each<i>file</i>operand (or standard input if no files were specified)will be processed in turn byreading data from the file untila record separator is seen(a newline characterby default),splitting the current record into fields using the current value of<b>FS</b>according to the rules in<xref href=awkre><a href="#tag_000_000_108_004">Regular Expressions</a></xref>,evaluating each pattern in theprogram in the order of occurrence,and executing the action associated with eachpattern that matches the current record.The action for a matching pattern will be executed beforeevaluating subsequent patterns.Last, the actions associated with all<b>END</b>patterns will be executed in the order they occur in the program.<h5><a name = "tag_000_000_108_002">&nbsp;</a>Expressions in awk</h5><xref type="5" name="awkexpr"></xref>Expressionsdescribe computations used in<i>patterns</i>and<i>actions.</i>In the following table,valid expression operations are given in groupsfrom highest precedence first to lowest precedence last,with equal-precedence operators grouped between horizontal lines.In expression evaluation,where the grammar is formally ambiguous,higher precedence operators will be evaluatedbefore lower precedence operators.In this table<i>expr</i>,<i>expr1</i>,<i>expr2</i>and<i>expr3</i>represent any expression,while<i>lvalue</i>represents any entity that can be assigned to (that is, on the left sideof an assignment operator).The precise syntax of expressions is given in<xref href=awkgram><a href="#tag_000_000_108_016">Grammar</a></xref>.<pre><table  bordercolor=#000000 border=1 align=center><tr valign=top><th align=center><b>Syntax</b><th align=center><b>Name</b><th align=center><b>Type of Result</b><th align=center><b>Associativity</b><tr valign=top><td align=left>( <i>expr</i> )<td align=left>Grouping<td align=left>type of <i>expr</i><td align=left>n/a<tr valign=top><td align=left>$<i>expr</i><td align=left>Field reference<td align=left>string<td align=left>n/a<tr valign=top><td align=left>++ <i>lvalue</i><td align=left>Pre-increment<td align=left>numeric<td align=left>n/a<tr valign=top><td align=left>-- <i>lvalue</i><td align=left>Pre-decrement<td align=left>numeric<td align=left>n/a<tr valign=top><td align=left><i>lvalue</i> ++<td align=left>Post-increment<td align=left>numeric<td align=left>n/a<tr valign=top><td align=left><i>lvalue</i> --<td align=left>Post-decrement<td align=left>numeric<td align=left>n/a<tr valign=top><td align=left><i>expr</i>^<i>expr</i><td align=left>Exponentiation<td align=left>numeric<td align=left>right<tr valign=top><td align=left>! <i>expr</i><td align=left>Logical not<td align=left>numeric

⌨️ 快捷键说明

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