📄 gawk.1
字号:
.ds PX \s-1POSIX\s+1.ds UX \s-1UNIX\s+1.ds AN \s-1ANSI\s+1.TH AWK 1 "April 15 1993" "Free Software Foundation" "Utility Commands".SH NAMEawk \- pattern scanning and processing language.SH SYNOPSIS.B awk[ POSIX or GNU style options ].B \-f.I program-file[.B \-\^\-] file .\^.\^..br.B awk[ POSIX or GNU style options ][.B \-\^\-].I program-textfile .\^.\^..SH DESCRIPTION.I Gawkis the GNU Project's implementation of the AWK programming language.In the 4.4BSD distribution, it is installed as.IR awk .It conforms to the definition of the language inthe \*(PX 1003.2 Command Language And Utilities Standard.This version in turn is based on the description in.IR "The AWK Programming Language" ,by Aho, Kernighan, and Weinberger,with the additional features defined in the System V Release 4 versionof \*(UX.IR awk ..I Gawkalso provides some GNU-specific extensions..PPThe command line consists of options to.I gawkitself, the AWK program text (if not supplied via the.B \-for.B \-\^\-fileoptions), and values to be madeavailable in the.B ARGCand.B ARGVpre-defined AWK variables..SH OPTIONS.PP.I Gawkoptions may be either the traditional \*(PX one letter options,or the GNU style long options. \*(PX style options start with a single ``\-'',while GNU long options start with ``\-\^\-''.GNU style long options are provided for both GNU-specific features andfor \*(PX mandated features. Other implementations of the AWK languageare likely to only accept the traditional one letter options..PPFollowing the \*(PX standard,.IR gawk -specificoptions are supplied via arguments to the.B \-Woption. Multiple.B \-Woptions may be supplied, or multiple arguments may be supplied togetherif they are separated by commas, or enclosed in quotes and separatedby white space.Case is ignored in arguments to the.B \-Woption.Each.B \-Woption has a corresponding GNU style long option, as detailed below..PP.I Gawkaccepts the following options..TP.PD 0.BI \-F " fs".TP.PD.BI \-\^\-field-separator= fsUse.I fsfor the input field separator (the value of the.B FSpredefinedvariable)..TP.PD 0\fB\-v\fI var\fB\^=\^\fIval\fR.TP.PD\fB\-\^\-assign=\fIvar\fB\^=\^\fIval\fRAssign the value.IR val ,to the variable.IR var ,before execution of the program begins.Such variable values are available to the.B BEGINblock of an AWK program..TP.PD 0.BI \-f " program-file".TP.PD.BI \-\^\-file= program-fileRead the AWK program source from the file.IR program-file ,instead of from the first command line argument.Multiple.B \-f(or.BR \-\^\-file )options may be used..TP \w'\fB\-\^\-copyright\fR'u+1n.PD 0.B "\-W compat".TP.PD.B \-\^\-compatRun in.I compatibilitymode. In compatibility mode,.I gawkbehaves identically to \*(UX.IR awk ;none of the GNU-specific extensions are recognized.See.BR "GNU EXTENSIONS" ,below, for more information..TP.PD 0.B "\-W copyleft".TP.PD 0.B "\-W copyright".TP.PD 0.B \-\^\-copyleft.TP.PD.B \-\^\-copyrightPrint the short version of the GNU copyright information message onthe standard error output..TP.PD 0.B "\-W help".TP.PD 0.B "\-W usage".TP.PD 0.B \-\^\-help.TP.PD.B \-\^\-usagePrint a relatively short summary of the available options onthe standard error output..TP.PD 0.B "\-W lint".TP.PD 0.B \-\^\-lintProvide warnings about constructs that aredubious or non-portable to other AWK implementations..ig.\" This option is left undocumented, on purpose..TP.PD 0.B "\-W nostalgia".TP.PD.B \-\^\-nostalgiaProvide a moment of nostalgia for long time.I awkusers....TP.PD 0.B "\-W posix".TP.PD.B \-\^\-posixThis turns on.I compatibility mode, with the following additional restrictions:.RS.TP \w'\(bu'u+1n\(bu.B \exescape sequences are not recognized..TP\(buThe synonym.B funcfor the keyword.B functionis not recognized..TP\(buThe operators.B **and.B **=cannot be used in place of.B ^and.BR ^= ..RE.TP.PD 0.BI "\-W source=" program-text.TP.PD.BI \-\^\-source= program-textUse.I program-textas AWK program source code.This option allows the easy intermixing of library functions (used via the .B \-fand.B \-\^\-fileoptions) with source code entered on the command line.It is intended primarily for medium to large size AWK programs usedin shell scripts..sp .5The.B "\-W source="form of this option uses the rest of the command line argument for.IR program-text ;no other options to.B \-Wwill be recognized in the same argument..TP.PD 0.B "\-W version".TP.PD.B \-\^\-versionPrint version information for this particular copy of.I gawkon the standard error output.This is useful mainly for knowing if the current copy of.I gawkon your systemis up to date with respect to whatever the Free Software Foundationis distributing..TP.B \-\^\-Signal the end of options. This is useful to allow further arguments to theAWK program itself to start with a ``\-''.This is mainly for consistency with the argument parsing convention usedby most other \*(PX programs..PPAny other options are flagged as illegal, but are otherwise ignored..SH AWK PROGRAM EXECUTION.PPAn AWK program consists of a sequence of pattern-action statementsand optional function definitions..RS.PP\fIpattern\fB { \fIaction statements\fB }\fR.br\fBfunction \fIname\fB(\fIparameter list\fB) { \fIstatements\fB }\fR.RE.PP.I Gawkfirst reads the program source from the.IR program-file (s)if specified, or from the first non-option argument on the command line.The.B \-foption may be used multiple times on the command line..I Gawkwill read the program text as if all the.IR program-file shad been concatenated together. This is useful for building librariesof AWK functions, without having to include them in each new AWKprogram that uses them. To use a library function in a file from aprogram typed in on the command line, specify.B /dev/ttyas one of the.IR program-file s,type your program, and end it with a.B ^D(control-d)..PPThe environment variable.B AWKPATHspecifies a search path to use when finding source files named withthe .B \-foption. If this variable does not exist, the default path is\fB".:/usr/lib/awk:/usr/local/lib/awk"\fR.If a file name given to the.B \-foption contains a ``/'' character, no path search is performed..PP.I Gawkexecutes AWK programs in the following order.First,.I gawkcompiles the program into an internal form.Next, all variable assignments specified via the.B \-voption are performed. Then,.I gawkexecutes the code in the.B BEGINblock(s) (if any),and then proceeds to readeach file named in the.B ARGVarray.If there are no files named on the command line,.I gawkreads the standard input..PPIf a filename on the command line has the form.IB var = valit is treated as a variable assignment. The variable.I varwill be assigned the value.IR val .(This happens after any.B BEGINblock(s) have been run.)Command line variable assignmentis most useful for dynamically assigning values to the variablesAWK uses to control how input is broken into fields and records. Itis also useful for controlling state if multiple passes are needed overa single data file..PPIf the value of a particular element of.B ARGVis empty (\fB""\fR),.I gawkskips over it..PPFor each line in the input,.I gawktests to see if it matches any.I patternin the AWK program.For each pattern that the line matches, the associated.I actionis executed.The patterns are tested in the order they occur in the program..PPFinally, after all the input is exhausted,.I gawkexecutes the code in the.B ENDblock(s) (if any)..SH VARIABLES AND FIELDSAWK variables are dynamic; they come into existence when they arefirst used. Their values are either floating-point numbers or strings,or both,depending upon how they are used. AWK also has one dimensionalarrays; multiply dimensioned arrays may be simulated.Several pre-defined variables are set as a programruns; these will be described as needed and summarized below..SS Fields.PPAs each input line is read,.I gawksplits the line into.IR fields ,using the value of the.B FSvariable as the field separator.If.B FSis a single character, fields are separated by that character.Otherwise,.B FSis expected to be a full regular expression.In the special case that.B FSis a single blank, fields are separatedby runs of blanks and/or tabs.Note that the value of.B IGNORECASE(see below) will also affect how fields are split when.B FSis a regular expression..PPIf the.B FIELDWIDTHSvariable is set to a space separated list of numbers, each field isexpected to have fixed width, and.I gawkwill split up the record using the specified widths. The value of.B FSis ignored.Assigning a new value to.B FSoverrides the use of.BR FIELDWIDTHS ,and restores the default behavior..PPEach field in the input line may be referenced by its position,.BR $1 ,.BR $2 ,and so on..B $0is the whole line. The value of a field may be assigned to as well.Fields need not be referenced by constants:.RS.PP.ft Bn = 5.brprint $n.ft R.RE.PPprints the fifth field in the input line.The variable.B NFis set to the total number of fields in the input line..PPReferences to non-existent fields (i.e., fields after.BR $NF )produce the null-string. However, assigning to a non-existent field(e.g., .BR "$(NF+2) = 5" )will increase the value of.BR NF ,create any intervening fields with the null string as their value, andcause the value of.B $0to be recomputed, with the fields being separated by the value of.BR OFS ..SS Built-in Variables.PPAWK's built-in variables are:.PP.TP \w'\fBFIELDWIDTHS\fR'u+1n.B ARGCThe number of command line arguments (does not include options to.IR gawk ,or the program source)..TP.B ARGINDThe index in.B ARGVof the current file being processed..TP.B ARGVArray of command line arguments. The array is indexed from0 to.B ARGC\- 1.Dynamically changing the contents of.B ARGVcan control the files used for data..TP.B CONVFMTThe conversion format for numbers, \fB"%.6g"\fR, by default..TP.B ENVIRONAn array containing the values of the current environment.The array is indexed by the environment variables, each element beingthe value of that variable (e.g., \fBENVIRON["HOME"]\fP might be.BR /u/arnold ).Changing this array does not affect the environment seen by programs which.I gawkspawns via redirection or the.B system()function.(This may change in a future version of.IR gawk .).\" but don't hold your breath....TP.B ERRNOIf a system error occurs either doing a redirection for.BR getline ,during a read for.BR getline ,or during a.BR close ,then.B ERRNOwill containa string describing the error..TP.B FIELDWIDTHSA white-space separated list of fieldwidths. When set,.I gawkparses the input into fields of fixed width, instead of using thevalue of the.B FSvariable as the field separator.The fixed field width facility is still experimental; expect thesemantics to change as.I gawkevolves over time..TP.B FILENAMEThe name of the current input file.If no files are specified on the command line, the value of.B FILENAMEis ``\-''..TP.B FNRThe input record number in the current input file..TP.B FSThe input field separator, a blank by default..TP.B IGNORECASEControls the case-sensitivity of all regular expression operations. If.B IGNORECASEhas a non-zero value, then pattern matching in rules,field splitting with.BR FS ,regular expressionmatching with.B ~and.BR !~ ,and the.BR gsub() ,.BR index() ,.BR match() ,.BR split() ,and.B sub()pre-defined functions will all ignore case when doing regular expressionoperations. Thus, if.B IGNORECASEis not equal to zero,.B /aB/matches all of the strings \fB"ab"\fP, \fB"aB"\fP, \fB"Ab"\fP,and \fB"AB"\fP.As with all AWK variables, the initial value of.B IGNORECASEis zero, so all regular expression operations are normally case-sensitive..TP.B NFThe number of fields in the current input record..TP.B NRThe total number of input records seen so far..TP.B OFMTThe output format for numbers, \fB"%.6g"\fR, by default..TP.B OFSThe output field separator, a blank by default..TP.B ORSThe output record separator, a newline by default..TP.B RSThe input record separator, a newline by default..B RSis exceptional in that only the first character of its stringvalue is used for separating records.(This will probably change in a future release of.IR gawk .)If.B RSis set to the null string, then records are separated byblank lines.When.B RSis set to the null string, then the newline character always acts asa field separator, in addition to whatever value.B FSmay have..TP.B RSTARTThe index of the first character matched by.BR match() ;0 if no match..TP.B RLENGTHThe length of the string matched by.BR match() ;\-1 if no match..TP.B SUBSEPThe character used to separate multiple subscripts in arrayelements, \fB"\e034"\fR by default..SS Arrays.PPArrays are subscripted with an expression between square brackets.RB ( [ " and " ] ).If the expression is an expression list.RI ( expr ", " expr " ...)"then the array subscript is a string consisting of theconcatenation of the (string) value of each expression,separated by the value of the.B SUBSEPvariable.This facility is used to simulate multiply dimensionedarrays. For example:.PP.RS.ft Bi = "A" ;\^ j = "B" ;\^ k = "C".brx[i, j, k] = "hello, world\en".ft R.RE.PPassigns the string \fB"hello, world\en"\fR to the element of the array.B xwhich is indexed by the string \fB"A\e034B\e034C"\fR. All arrays in AWKare associative, i.e., indexed by string values..PPThe special operator.B inmay be used in an.B ifor.B whilestatement to see if an array has an index consisting of a particularvalue.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -