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

📄 awk.9

📁 minix操作系统最新版本(3.1.1)的源代码
💻 9
字号:
.so mnx.mac.TH AWK 9.CD "awk \(en pattern matching language".SX "awk \fIrules\fR [\fIfile\fR] ....FL "\fR(none)".EX "awk rules input" "Process \fIinput\fR according to \fIrules\fR".EX "awk rules \(en  >out" "Input from terminal, output to \fIout\fR".PPAWK is a programming language devised by Aho, Weinberger, and Kernighanat Bell Labs (hence the name).\fIAwk\fR programs search files forspecific patterns and performs \*(OQactions\*(CQ for every occurrenceof these patterns.  The patterns can be \*(OQregular expressions\*(CQas used in the \fIed\fR editor.  The actions are expressedusing a subset of the C language..PPThe patterns and actions are usually placed in a \*(OQrules\*(CQ filewhose name must be the first argument in the command line,preceded by the flag \fB\(enf\fR.  Otherwise, the first argument on thecommand line is taken to be a string containing the rulesthemselves. All other arguments are taken to be the names of textfiles on which the rules are to be applied, with \fB\(en\fR being thestandard input.  To take rules from the standard input, use \fB\(enf \(en\fR..PPThe command:.HS.Cx "awk  rules  prog.\d\s+2*\s0\u".HSwould read the patterns and actions rules from the file \fIrules\fRand apply them to all the arguments..PPThe general format of a rules file is:.HS~~~<pattern> { <action> }~~~<pattern> { <action> }~~~....HSThere may be any number of these <pattern> { <action> }sequences in the rules file.  \fIAwk\fR reads a line of input fromthe current input file and applies every <pattern> { <action> }in sequence to the line..PPIf the <pattern> corresponding to any { <action> } is missing,the action is applied to every line of input.  The default{ <action> } is to print the matched input line..SS "Patterns".PPThe <pattern>s may consist of any valid C expression.  If the<pattern> consists of two expressions separated by a comma, itis taken to be a range and the <action> is performed on alllines of input that match the range.  <pattern>s may contain\*(OQregular expressions\*(CQ delimited by an @ symbol.  Regularexpressions can be thought of as a generalized \*(OQwildcard\*(CQstring matching mechanism, similar to that used by manyoperating systems to specify file names.  Regular expressionsmay contain any of the following characters:.HS.in +0.75i.ta +0.5i.ti -0.5ix	An ordinary character.ti -0.5i\\	The backslash quotes any character.ti -0.5i^	A circumflex at the beginning of an expr matches the beginning of a line..ti -0.5i$	A dollar-sign at the end of an expression matches the end of a line..ti -0.5i\&.	A period matches any single character except newline..ti -0.5i*	An expression followed by an asterisk matches zero or more occurrencesof that expression: \*(OQfo*\*(CQ matches \*(OQf\*(CQ, \*(OQfo\*(CQ, \*(OQfoo\*(CQ, \*(OQfooo\*(CQ, etc..ti -0.5i+	An expression followed by a plus sign matches one or more occurrences of that expression: \*(OQfo+\*(CQ matches \*(OQfo\*(CQ, \*(OQfoo\*(CQ, \*(OQfooo\*(CQ, etc..ti -0.5i[]	A string enclosed in square brackets matches any single character in that string, but no others.  If the first character in the string is a circumflex, the expression matches any character except newline and the characters in the string.  For example, \*(OQ[xyz]\*(CQ matches \*(OQxx\*(CQ and \*(OQzyx\*(CQ, while \*(OQ[^xyz]\*(CQ matches \*(OQabc\*(CQ but not \*(OQaxb\*(CQ.  A range of characters may be specified by two characters separated by \*(OQ-\*(CQ..in -0.75i.SS "Actions".PPActions are expressed as a subset of the C language.  Allvariables are global and default to int's if not formallydeclared.  Only char's and int's and pointers and arrays ofchar and int are allowed.  \fIAwk\fR allows only decimal integerconstants to be used\(emno hex (0xnn) or octal (0nn). Stringand character constants may contain all of the special Cescapes (\\n, \\r, etc.)..PP\fIAwk\fR supports the \*(OQif\*(CQ, \*(OQelse\*(CQ, \*(OQwhile\*(CQ and \*(OQbreak\*(CQ flow ofcontrol constructs, which behave exactly as in C..PPAlso supported are the following unary and binary operators,listed in order from highest to lowest precedence:.HS.ta 0.25i 1.75i 3.0i.nf\fB	Operator	Type	Associativity\fR	() []	unary	left to right.tr ~~	! ~ ++ \(en\(en \(en * &	unary	right to left.tr ~	* / %	binary	left to right	+ \(en	binary	left to right	<< >>	binary	left to right	< <= > >=	binary	left to right	== !=	binary	left to right	&	binary	left to right	^	binary	left to right	|	binary	left to right	&&	binary	left to right	||	binary	left to right	=	binary	right to left.fi.HSComments are introduced by a '#' symbol and are terminated bythe first newline character.  The standard \*(OQ/*\*(CQ and \*(OQ*/\*(CQcomment delimiters are not supported and will result in asyntax error..SP 0.5.SS "Fields".SP 0.5.PPWhen \fIawk\fR reads a line from the current input file, therecord is automatically separated into \*(OQfields.\*(CQ  A field issimply a string of consecutive characters delimited by eitherthe beginning or end of line, or a \*(OQfield separator\*(CQ character.Initially, the field separators are the space and tab character.The special unary operator '$' is used to reference one of thefields in the current input record (line).  The fields arenumbered sequentially starting at 1.  The expression \*(OQ$0\*(CQreferences the entire input line..PPSimilarly, the \*(OQrecord separator\*(CQ is used to determine the endof an input \*(OQline,\*(CQ initially the newline character.  The fieldand record separators may be changed programatically by one ofthe actions and will remain in effect until changed again..PPMultiple (up to 10) field separators are allowed at a time, butonly one record separator..PPFields behave exactly like strings; and can be used in the samecontext as a character array.  These \*(OQarrays\*(CQ can be consideredto have been declared as:.SP 0.15.HS~~~~~char ($n)[ 128 ];.HS.SP 0.15In other words, they are 128 bytes long.  Notice that theparentheses are necessary because the operators [] and $associate from right to left; without them, the statementwould have parsed as:.HS.SP 0.15~~~~~char $(1[ 128 ]);.HS.SP 0.15which is obviously ridiculous..PPIf the contents of one of these field arrays is altered, the\*(OQ$0\*(CQ field will reflect this change.  For example, thisexpression:.HS.SP 0.15~~~~~*$4 = 'A';.HS.SP 0.15will change the first character of the fourth field to an upper-case letter 'A'.  Then, when the following input line:.HS.SP 0.15~~~~~120 PRINT "Name         address        Zip".SP 0.15.HSis processed, it would be printed as:.HS.SP 0.15~~~~~120 PRINT "Name         Address        Zip".HS.SP 0.15Fields may also be modified with the strcpy() function (seebelow).  For example, the expression:.HS~~~~~strcpy( $4, "Addr." );.HSapplied to the same line above would yield:.HS~~~~~120 PRINT "Name         Addr.        Zip".HS.SS "Predefined Variables".PPThe following variables are pre-defined:.HS.in +1.5i.ta +1.25i.ti -1.25iFS	Field separator (see below)..ti -1.25iRS	Record separator (see below also)..ti -1.25iNF	Number of fields in current input record (line)..ti -1.25iNR	Number of records processed thus far..ti -1.25iFILENAME	Name of current input file..ti -1.25iBEGIN	A special <pattern> that matches the beginning of input text..ti -1.25iEND	A special <pattern> that matches the end of input text..in -1.5i.HS\fIAwk\fR also provides some useful built-in functions for stringmanipulation and printing:.HS.in +1.5i.ta +1.25i.ti -1.25iprint(arg)	Simple printing of strings only, terminated by '\\n'..ti -1.25iprintf(arg...)	Exactly the printf() function from C..ti -1.25igetline()	Reads the next record and returns 0 on end of file..ti -1.25inextfile()	Closes the current input file and begins processing the next file.ti -1.25istrlen(s)	Returns the length of its string argument..ti -1.25istrcpy(s,t)	Copies the string \*(OQt\*(CQ to the string \*(OQs\*(CQ..ti -1.25istrcmp(s,t)	Compares the \*(OQs\*(CQ to \*(OQt\*(CQ and returns 0 if they match..ti -1.25itoupper(c)	Returns its character argument converted to upper-case..ti -1.25itolower(c)	Returns its character argument converted to lower-case..ti -1.25imatch(s,@re@)	Compares the string \*(OQs\*(CQ to the regular expression \*(OQre\*(CQ and returns the number of matches found (zero if none)..in -1.5i.SS "Authors".PP\fIAwk\fR was written by Saeko Hirabauashi and Kouichi Hirabayashi.

⌨️ 快捷键说明

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