awk - Unix, Linux Command---reference
For each record in the input,?gawk?tests to see if it matches any?pattern?in the?AWKprogram. For each pattern that the record matches,the associated?action?is executed. The patterns are tested in the order they occur in the program. Finally,after all the input is exhausted,?gawk?executes the code in the?END?block(s) (if any). 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 pre-defined variables are set as a program runs; these will be described as needed and summarized below. RecordsNormally,records are separated by newline characters. You can control how records are separated by assigning values to the built-in variable?RS. If?RS?is any single character,that character separates records. Otherwise,?RS?is a regular expression. Text in the input that matches this regular expression separates the record. However,in compatibility mode,only the first character of its string value is used for separating records. If?RS?is set to the null string,then records are separated by blank lines. WhenRS?is set to the null string,the newline character always acts as a field separator,in addition to whatever value?FS?may have. FieldsAs each input record is read,?gawk?splits the record into?fields,using the value of the?FSvariable as the field separator. If?FS?is a single character,fields are separated by that character. If?FS?is the null string,then each individual character becomes a separate field. Otherwise,?FS?is expected to be a full regular expression. In the special case thatFS?is a single space,fields are separated by runs of spaces and/or tabs and/or newlines. (But see the discussion of?--posix,below).?NOTE:?The value of?IGNORECASE(see below) also affects how fields are split when?FS?is a regular expression,and how records are separated when?RS?is a regular expression. If the?FIELDWIDTHS?variable is set to a space separated list of numbers,each field is expected to have fixed width,and?gawk?splits up the record using the specified widths. The value of?FS?is ignored. Assigning a new value to?FS?overrides the use ofFIELDWIDTHS,and restores the default behavior. Each field in the input record may be referenced by its position,?$1,?$2,and so on.?$0?is the whole record. Fields need not be referenced by constants: n = 5?print $n prints the fifth field in the input record. The variable?NF?is set to the total number of fields in the input record. References to non-existent fields (i.e. fields after?$NF) produce the null-string. However,assigning to a non-existent field (e.g.,?$(NF+2) = 5) increases the value of?NF,creates any intervening fields with the null string as their value,and causes 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. Decrementing?NF?causes the values of fields past the new value to be lost,and the value of?$0?to be recomputed,with the fields being separated by the value of?OFS. Assigning a value to an existing field causes the whole record to be rebuilt when?$0?is referenced. Similarly,assigning a value to?$0?causes the record to be resplit,creating new values for the fields. Built-in VariablesGawk’s built-in variables are: <table class="src" border="1" cellspacing="0" cellpadding="5"> | Description | gawk,or the program source).</tr>
---|