Home Explore Blog CI



man-pages

3rd chunk of `awk.man`
0943e688535afddf7e9c1bfdc3ff13f1dcb9ab0d288ace5a0000000100000fbb
 Numeric constants can be integer like -2, decimal like 1.08, or in scientific notation like -1.1e4 or .28E-3.  All numbers are represented internally and all compu‐
       tations are done in floating point arithmetic.  So for example, the expression 0.2e2 == 20 is true and true is represented as 1.0.

       String constants are enclosed in double quotes.

                                                                                            "This is a string with a newline at the end.\n"

       Strings can be continued across a line by escaping (\) the newline.  The following escape sequences are recognized.

            \\        \
            \"        "
            \a        alert, ascii 7
            \b        backspace, ascii 8
            \t        tab, ascii 9
            \n        newline, ascii 10
            \v        vertical tab, ascii 11
            \f        formfeed, ascii 12
            \r        carriage return, ascii 13
            \ddd      1, 2 or 3 octal digits for ascii ddd
            \xhh      1 or 2 hex digits for ascii  hh

       If you escape any other character \c, you get \c, i.e., mawk ignores the escape.

       There are really three basic data types; the third is number and string which has both a numeric value and a string value at the same time.  User defined variables come into existence when  first  referenced  and  are
       initialized to null, a number and string value which has numeric value 0 and string value "".  Non‐trivial number and string typed data come from input and are typically stored in fields.  (See section 4).

       The type of an expression is determined by its context and automatic type conversion occurs if needed.  For example, to evaluate the statements

            y = x + 2  ;  z = x  "hello"

       The  value stored in variable y will be typed numeric.  If x is not numeric, the value read from x is converted to numeric before it is added to 2 and stored in y.  The value stored in variable z will be typed string,
       and the value of x will be converted to string if necessary and concatenated with "hello".  (Of course, the value and type stored in x is not changed by any conversions.)  A string expression is converted  to  numeric
       using its longest numeric prefix as with atof(3).  A numeric expression is converted to string by replacing expr with sprintf(CONVFMT, expr), unless expr can be represented on the host machine as an exact integer then
       it is converted to sprintf("%d", expr).  Sprintf() is an AWK built‐in that duplicates the functionality of sprintf(3), and CONVFMT is a built‐in variable used for internal conversion from number to string and initial‐
       ized to "%.6g".  Explicit type conversions can be forced, expr "" is string and expr+0 is numeric.

       To evaluate, expr1 rel‐op expr2, if both operands are numeric or number and string then the comparison is numeric; if both operands are string the comparison is string; if one operand is string, the non‐string operand
       is converted and the comparison is string.  The result is numeric, 1 or 0.

       In boolean contexts such as, if ( expr ) statement, a string expression evaluates true if and only if it is not the empty string ""; numeric values if and only if not numerically zero.

   3. Regular expressions
       In the AWK language, records, fields and strings are often tested for matching a regular expression.  Regular expressions are enclosed in slashes, and

            expr ~ /r/

       is an AWK expression that evaluates to 1 if expr “matches” r, which means a substring of expr is in the set of strings defined by r.  With no match the expression evaluates to 0; replacing ~ with the “not match” oper‐
       ator, !~ , reverses the meaning.  As  pattern‐action pairs,

            /r/ { action }   and   $0 ~ /r/ { action }

       are  the  same, and for each input record that matches r, action is executed.  In fact, /r/ is an AWK expression that is equivalent

Title: Data Types, Conversion, and Regular Expressions in AWK
Summary
This section discusses data types in AWK, including numeric (integer, decimal, scientific notation) and string constants, escape sequences, and automatic type conversions between numeric and string types based on context. It explains how expressions are evaluated and compared, and how regular expressions are used for pattern matching using the `~` and `!~` operators.