Home Explore Blog CI



man-pages

6th chunk of `awk.man`
3182ef32738141e4988c8324ec44a8f3ba2cacc241451ddf0000000100000fb9
 when first referenced and are initialized to null.

       New expressions are composed with the following operators in order of increasing precedence.

            assignment          =  +=  -=  *=  /=  %=  ^=
            conditional         ?  :
            logical or          ||
            logical and         &&
            array membership    in
            matching       ~   !~
            relational          <  >   <=  >=  ==  !=
            concatenation       (no explicit operator)
            add ops             +  -
            mul ops             *  /  %
            unary               +  -
            logical not         !
            exponentiation      ^
            inc and dec         ++ -- (both post and pre)
            field               $

       Assignment, conditional and exponentiation associate right to left; the other operators associate left to right.  Any expression can be parenthesized.

   6. Arrays
       Awk  provides one‐dimensional arrays.  Array elements are expressed as array[expr].  Expr is internally converted to string type, so, for example, A[1] and A["1"] are the same element and the actual index is "1".  Ar‐
       rays indexed by strings are called associative arrays.  Initially an array is empty; elements exist when first accessed.  An expression, expr in array evaluates to 1 if array[expr] exists, else to 0.

       There is a form of the for statement that loops over each index of an array.

            for ( var in array ) statement

       sets var to each index of array and executes statement.  The order that var transverses the indices of array is not defined.

       The statement, delete array[expr], causes array[expr] not to exist.  mawk supports the delete array feature, which deletes all elements of array.

       Multidimensional arrays are synthesized with concatenation using the built‐in variable SUBSEP.  array[expr1,expr2] is equivalent to array[expr1 SUBSEP expr2].  Testing for a multidimensional element uses  a  parenthe‐
       sized index, such as

            if ( (i, j) in A )  print A[i, j]

   7. Builtin‐variables
       The following variables are built‐in and initialized before program execution.

            ARGC   number of command line arguments.

            ARGV   array of command line arguments, 0..ARGC-1.

            CONVFMT
                   format for internal conversion of numbers to string, initially = "%.6g".

            ENVIRON
                   array indexed by environment variables.  An environment string, var=value is stored as ENVIRON[var] = value.

            FILENAME
                   name of the current input file.

            FNR    current record number in FILENAME.

            FS     splits records into fields as a regular expression.

            NF     number of fields in the current record.

            NR     current record number in the total input stream.

            OFMT   format for printing numbers; initially = "%.6g".

            OFS    inserted between fields on output, initially = " ".

            ORS    terminates each record on output, initially = "\n".

            RLENGTH
                   length set by the last call to the built‐in function, match().

            RS     input record separator, initially = "\n".

            RSTART index set by the last call to match().

            SUBSEP used to build multiple array subscripts, initially = "\034".

   8. Built‐in functions
       String functions

            gsub(r,s,t)  gsub(r,s)
                   Global  substitution,  every match of regular expression r in variable t is replaced by string s.  The number of replacements is returned.  If t is omitted, $0 is used.  An & in the replacement string s is
                   replaced by the matched substring of t.  \& and \\ put  literal & and \, respectively, in the replacement string.

            index(s,t)
                   If t is a substring of s, then the position where t starts is returned, else 0 is returned.

Title: Arrays, Built-in Variables, and Built-in Functions in AWK
Summary
This section explains arrays in AWK, including how elements are accessed, the concept of associative arrays, and looping through array indices with the `for` statement. It also covers deleting array elements. Multidimensional arrays are synthesized using `SUBSEP`. The section then details built-in variables like `ARGC`, `ARGV`, `CONVFMT`, `ENVIRON`, `FILENAME`, `FNR`, `FS`, `NF`, `NR`, `OFMT`, `OFS`, `ORS`, `RLENGTH`, `RS`, `RSTART`, and `SUBSEP`, explaining their purpose and initial values. Finally, it introduces built-in string functions such as `gsub` and `index` with explanations of their behavior and parameters.