man_de_awk
Différences
Ci-dessous, les différences entre deux révisions de la page.
| man_de_awk [2015/12/21 21:52] – créée simon | man_de_awk [2026/08/23 07:49] (Version actuelle) – supprimée - modification externe (Date inconnue) 127.0.0.1 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | ==== Man de la commande " | ||
| - | < | ||
| - | GAWK(1) | ||
| - | |||
| - | |||
| - | |||
| - | NAME | ||
| - | gawk - pattern scanning and processing language | ||
| - | |||
| - | SYNOPSIS | ||
| - | gawk [ POSIX or GNU style options ] -f program-file [ -- ] file ... | ||
| - | gawk [ POSIX or GNU style options ] [ -- ] program-text file ... | ||
| - | |||
| - | DESCRIPTION | ||
| - | | ||
| - | POSIX 1003.1 Standard. | ||
| - | | ||
| - | cific extensions. | ||
| - | |||
| - | The command line consists of options to gawk itself, the AWK program text (if not supplied via the -f or --file | ||
| - | | ||
| - | |||
| - | When gawk is invoked with the --profile option, it starts gathering profiling statistics from the execution of the program. | ||
| - | runs more slowly in this mode, and automatically produces an execution profile in the file awkprof.out when done. See the --pro‐ | ||
| - | file option, below. | ||
| - | |||
| - | Gawk also has an integrated debugger. An interactive debugging session can be started by supplying the --debug option to the com‐ | ||
| - | mand line. In this mode of execution, gawk loads the AWK source code and then prompts for debugging | ||
| - | debug AWK program source provided with the -f option. | ||
| - | |||
| - | OPTION FORMAT | ||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | = sign, with no intervening spaces, or they may be provided in the next command line argument. | ||
| - | as long as the abbreviation remains unique. | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | OPTIONS | ||
| - | Gawk accepts the following options. | ||
| - | | ||
| - | |||
| - | -f program-file | ||
| - | | ||
| - | Read the AWK program source from the file program-file, | ||
| - | --file) options may be used. | ||
| - | |||
| - | -F fs | ||
| - | | ||
| - | Use fs for the input field separator (the value of the FS predefined variable). | ||
| - | |||
| - | -v var=val | ||
| - | | ||
| - | Assign the value val to the variable var, before execution of the program begins. | ||
| - | the BEGIN rule of an AWK program. | ||
| - | |||
| - | -b | ||
| - | | ||
| - | Treat all input data as single-byte characters. In other words, don't pay any attention to the locale information when | ||
| - | attempting to process strings as multibyte characters. | ||
| - | |||
| - | -c | ||
| - | | ||
| - | Run in compatibility mode. In compatibility mode, gawk behaves identically to Brian Kernighan' | ||
| - | cific extensions are recognized. | ||
| - | |||
| - | -C | ||
| - | | ||
| - | Print the short version of the GNU copyright information message on the standard output and exit successfully. | ||
| - | |||
| - | | ||
| - | | ||
| - | Print a sorted list of global variables, their types and final values to file. If no file is provided, gawk uses a file | ||
| - | named awkvars.out in the current directory. | ||
| - | Having a list of all the global variables is a good way to look for typographical errors in your programs. | ||
| - | use this option | ||
| - | inadvertently use global variables that you meant to be local. | ||
| - | variable names like i, j, and so on.) | ||
| - | |||
| - | | ||
| - | | ||
| - | Enable | ||
| - | file argument specifies a file with a list of commands for the debugger to execute non-interactively. | ||
| - | |||
| - | -e program-text | ||
| - | | ||
| - | Use program-text as AWK program source code. This option allows the easy intermixing of library functions (used via the | ||
| - | -f and --file | ||
| - | programs used in shell scripts. | ||
| - | |||
| - | -E file | ||
| - | | ||
| - | Similar to -f, however, this is option is the last one processed. | ||
| - | CGI applications, | ||
| - | command-line variable assignments. | ||
| - | |||
| - | -g | ||
| - | | ||
| - | Scan and parse the AWK program, and generate a GNU .pot (Portable Object Template) format file on standard | ||
| - | entries for all localizable strings in the program. | ||
| - | for more information on .pot files. | ||
| - | |||
| - | -h | ||
| - | | ||
| - | options cause an immediate, successful exit.) | ||
| - | |||
| - | -i include-file | ||
| - | | ||
| - | Load an awk source library. | ||
| - | fails, another attempt will be made after appending the .awk suffix. | ||
| - | are eliminated), | ||
| - | |||
| - | -l lib | ||
| - | | ||
| - | Load a shared | ||
| - | search fails, another attempt will be made after appending the default | ||
| - | library initialization routine is expected to be named dl_load(). | ||
| - | |||
| - | -L [value] | ||
| - | | ||
| - | Provide | ||
| - | ment of fatal, lint warnings become fatal errors. | ||
| - | ment of cleaner AWK programs. | ||
| - | are issued. (This is not fully implemented yet.) | ||
| - | |||
| - | -M | ||
| - | | ||
| - | Force arbitrary precision arithmetic on numbers. This option has no effect if gawk is not compiled to use the GNU MPFR and | ||
| - | MP libraries. | ||
| - | |||
| - | -n | ||
| - | | ||
| - | Recognize octal and hexadecimal values in input data. Use this option with great caution! | ||
| - | |||
| - | -N | ||
| - | | ||
| - | This forces | ||
| - | requires this behavior, and gawk does so when --posix is in effect, the default is to follow traditional behavior and use | ||
| - | a period as the decimal point, even in locales where the period is not the decimal point character. | ||
| - | the default behavior, without the full draconian strictness of the --posix option. | ||
| - | |||
| - | | ||
| - | | ||
| - | Output a pretty printed version of the program to file. If no file is provided, gawk uses a file named awkprof.out in the | ||
| - | current directory. | ||
| - | |||
| - | -O | ||
| - | | ||
| - | Enable | ||
| - | and tail call elimination for recursive functions. The gawk maintainer hopes to add additional optimizations over time. | ||
| - | |||
| - | | ||
| - | | ||
| - | Start a profiling session, and send the profiling data to prof-file. | ||
| - | execution | ||
| - | tion. | ||
| - | |||
| - | -P | ||
| - | | ||
| - | This turns on compatibility mode, with the following additional restrictions: | ||
| - | |||
| - | · \x escape sequences are not recognized. | ||
| - | |||
| - | · Only space and tab act as field separators when FS is set to a single space, newline does not. | ||
| - | |||
| - | · You cannot continue lines after ? and :. | ||
| - | |||
| - | · The synonym func for the keyword function is not recognized. | ||
| - | |||
| - | · The operators ** and **= cannot be used in place of ^ and ^=. | ||
| - | |||
| - | -r | ||
| - | | ||
| - | Enable the use of interval expressions in regular expression matching (see Regular Expressions, | ||
| - | sions were not traditionally available in the AWK language. | ||
| - | tent with each other. | ||
| - | |||
| - | -S | ||
| - | | ||
| - | Runs gawk in sandbox mode, disabling the system() function, input redirection with getline, output redirection with print | ||
| - | and printf, | ||
| - | blocks a script from accessing local resources (except for the files specified on the command line). | ||
| - | |||
| - | -t | ||
| - | | ||
| - | Provide warnings about constructs that are not portable to the original version of UNIX awk. | ||
| - | |||
| - | -V | ||
| - | | ||
| - | Print version information for this particular copy of gawk on the standard output. | ||
| - | the current copy of gawk on your system is up to date with respect to whatever the Free Software Foundation is distribut‐ | ||
| - | ing. This is also useful when reporting bugs. (Per the GNU Coding Standards, these options cause an immediate, | ||
| - | ful exit.) | ||
| - | |||
| - | | ||
| - | provides consistency with the argument parsing convention used by most other POSIX programs. | ||
| - | |||
| - | In compatibility mode, any other options are flagged as invalid, but are otherwise ignored. | ||
| - | gram text has been supplied, unknown options are passed on to the AWK program in the ARGV array for processing. | ||
| - | larly useful for running AWK programs via the “#!” executable interpreter mechanism. | ||
| - | |||
| - | For POSIX compatibility, | ||
| - | |||
| - | AWK PROGRAM EXECUTION | ||
| - | An AWK program consists of a sequence of pattern-action statements and optional function definitions. | ||
| - | |||
| - | @include " | ||
| - | @load " | ||
| - | pattern | ||
| - | function name(parameter list) { statements } | ||
| - | |||
| - | Gawk first reads the program source from the program-file(s) if specified, from arguments to --source, or from the first non- | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | In addition, lines beginning with @include may be used to include other source files into your program, making library | ||
| - | | ||
| - | |||
| - | Lines beginning with @load may be used to load shared libraries into your program. | ||
| - | |||
| - | | ||
| - | this variable does not exist, the default path is " | ||
| - | gawk was built and installed.) | ||
| - | |||
| - | | ||
| - | | ||
| - | was built and installed.) | ||
| - | |||
| - | | ||
| - | Next, gawk compiles the program into an internal form. Then, gawk executes the code in the BEGIN rule(s) (if any), and then pro‐ | ||
| - | | ||
| - | the standard input. | ||
| - | |||
| - | If a filename on the command line has the form var=val it is treated as a variable assignment. | ||
| - | the value val. (This happens after any BEGIN rule(s) have been run.) Command line variable assignment is most useful for dynam‐ | ||
| - | | ||
| - | | ||
| - | |||
| - | If the value of a particular element of ARGV is empty ("" | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | For each record in the input, gawk tests to see if it matches any pattern in the AWK program. | ||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | the --posix or --traditional options is given, then gawk reverts to treating directories on the command line as a fatal error. | ||
| - | |||
| - | VARIABLES, RECORDS AND FIELDS | ||
| - | | ||
| - | | ||
| - | | ||
| - | these are described as needed and summarized below. | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | Text in the input that matches this regular expression separates the record. | ||
| - | | ||
| - | blank lines. | ||
| - | value FS may have. | ||
| - | |||
| - | | ||
| - | | ||
| - | FS is a single character, fields are separated by that character. | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | If the FIELDWIDTHS variable is set to a space separated list of numbers, each field is expected to have fixed width, | ||
| - | | ||
| - | use of FIELDWIDTHS. | ||
| - | |||
| - | | ||
| - | that regular expression. In this case, the regular expression describes the fields themselves, instead of the text that separates | ||
| - | the fields. | ||
| - | |||
| - | Each field in the input record may be referenced by its position: $1, $2, and so on. $0 is the whole record. | ||
| - | | ||
| - | |||
| - | n = 5 | ||
| - | print $n | ||
| - | |||
| - | | ||
| - | |||
| - | The variable NF is set to the total number of fields in the input record. | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | cause a fatal error. | ||
| - | | ||
| - | |||
| - | | ||
| - | to $0 causes the record to be resplit, creating new values for the fields. | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | can control the files used for data. | ||
| - | |||
| - | | ||
| - | input files, output files, or all files, respectively, | ||
| - | | ||
| - | all files should use binary I/O. Any other string value is treated as " | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | not affect the environment seen by programs which gawk spawns via redirection or the system() function. | ||
| - | |||
| - | | ||
| - | ERRNO will contain a string describing the error. | ||
| - | |||
| - | | ||
| - | using the value of the FS variable as the field separator. | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | where the fields match the regular expression, instead of using the value of the FS variable as the field separator. | ||
| - | See Fields, above. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | Thus, if IGNORECASE is not equal to zero, /aB/ matches all of the strings " | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | The default time format string for strftime(). | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | or " | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | one of the following: | ||
| - | |||
| - | " | ||
| - | The identifier is an array. | ||
| - | |||
| - | " | ||
| - | The identifier is an extension function loaded via @load. | ||
| - | |||
| - | " | ||
| - | The identifier is a scalar. | ||
| - | |||
| - | " | ||
| - | The identifier is untyped (could be used as a scalar or array, gawk doesn' | ||
| - | |||
| - | " | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | If this element exists in PROCINFO, then its value controls the order in which array elements are | ||
| - | | ||
| - | " | ||
| - | " | ||
| - | ison function defined as follows: | ||
| - | |||
| - | function cmp_func(i1, | ||
| - | |||
| - | where i1 and i2 are the indices, and v1 and v2 are the corresponding values of the two elements | ||
| - | | ||
| - | how the elements of the array are to be ordered. | ||
| - | |||
| - | | ||
| - | The timeout in milliseconds for reading data from input, where input is a redirection string or a | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | entry is not present if MPFR support is not compiled into gawk. | ||
| - | |||
| - | | ||
| - | The version of the GNU MP library used for arbitrary precision | ||
| - | entry is not present if MPFR support is not compiled into gawk. | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | The minimum precision allowed by the GNU MPFR library for arbitrary precision floating-point num‐ | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | is not available. | ||
| - | |||
| - | | ||
| - | The minor version of the extension API. This entry is not present if loading dynamic | ||
| - | is not available. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | for roundTiesToAway. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | RS. | ||
| - | |||
| - | | ||
| - | one.) | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | may be used for indirect access to read or write the value of a variable: | ||
| - | |||
| - | foo = 5 | ||
| - | SYMTAB[" | ||
| - | print foo # prints 4 | ||
| - | |||
| - | The isarray() function may be used to test if an element in SYMTAB is an array. | ||
| - | with the SYMTAB array. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | the value of the SUBSEP variable. | ||
| - | |||
| - | i = " | ||
| - | x[i, j, k] = " | ||
| - | |||
| - | | ||
| - | AWK are associative, | ||
| - | |||
| - | The special operator in may be used to test if an array has an index consisting of a particular value: | ||
| - | |||
| - | if (val in array) | ||
| - | print array[val] | ||
| - | |||
| - | If the array has multiple subscripts, use (i, j) in array. | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | An element may be deleted from an array using the delete statement. | ||
| - | | ||
| - | |||
| - | gawk supports true multidimensional arrays. It does not require that such arrays be ``rectangular'' | ||
| - | |||
| - | a[1] = 5 | ||
| - | a[2][1] = 6 | ||
| - | a[2][2] = 7 | ||
| - | |||
| - | | ||
| - | as in the second argument to split()). | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | upon its context. | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | When a string must be converted to a number, the conversion is accomplished using strtod(3). | ||
| - | | ||
| - | ever, even though all numbers in AWK are floating-point, | ||
| - | |||
| - | CONVFMT = " | ||
| - | a = 12 | ||
| - | b = a "" | ||
| - | |||
| - | the variable b has a string value of " | ||
| - | |||
| - | NOTE: When operating in POSIX mode (such as with the --posix option), beware that locale settings may interfere with the way dec‐ | ||
| - | | ||
| - | | ||
| - | |||
| - | Gawk performs comparisons as follows: If two variables are numeric, they are compared numerically. | ||
| - | | ||
| - | value is converted to a string and a string comparison is performed. | ||
| - | |||
| - | Note that string constants, such as " | ||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | Octal and Hexadecimal Constants | ||
| - | | ||
| - | to decimal 9, and the hexadecimal value 0x11 is equal to decimal 17. | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | \xhex digits | ||
| - | The character represented by the string of hexadecimal digits following the \x. As in ISO C, all following hexadecimal dig‐ | ||
| - | its are considered part of the escape sequence. | ||
| - | E.g., " | ||
| - | |||
| - | \ddd The character represented by the 1-, 2-, or 3-digit sequence of octal digits. | ||
| - | acter. | ||
| - | |||
| - | | ||
| - | |||
| - | The escape sequences may also be used inside constant regular expressions (e.g., /[ \t\f\n\r\v]/ | ||
| - | |||
| - | In compatibility mode, the characters represented by octal and hexadecimal escape sequences are treated literally | ||
| - | | ||
| - | |||
| - | PATTERNS AND ACTIONS | ||
| - | | ||
| - | | ||
| - | is executed for every single record of input. | ||
| - | |||
| - | { print } | ||
| - | |||
| - | which prints the entire record. | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | part of a pattern-action pair (the usual case), and to the pattern-action statements themselves. | ||
| - | |||
| - | | ||
| - | AWK patterns may be one of the following: | ||
| - | |||
| - | BEGIN | ||
| - | END | ||
| - | BEGINFILE | ||
| - | ENDFILE | ||
| - | /regular expression/ | ||
| - | relational expression | ||
| - | pattern && pattern | ||
| - | pattern || pattern | ||
| - | pattern ? pattern : pattern | ||
| - | (pattern) | ||
| - | ! pattern | ||
| - | pattern1, pattern2 | ||
| - | |||
| - | | ||
| - | are merged as if all the statements had been written in a single BEGIN rule. They are executed before any of the input is read. | ||
| - | | ||
| - | BEGIN and END patterns cannot be combined with other patterns in pattern expressions. | ||
| - | | ||
| - | |||
| - | | ||
| - | line input file and after reading the last record of each file. Inside the BEGINFILE rule, the value of ERRNO will be the empty | ||
| - | | ||
| - | skip it. If that is not done, gawk produces its usual fatal error for files that cannot be opened. | ||
| - | |||
| - | For /regular expression/ patterns, the associated statement is executed for each input record that matches | ||
| - | | ||
| - | |||
| - | | ||
| - | tain fields match certain regular expressions. | ||
| - | |||
| - | The &&, ||, and ! operators are logical AND, logical OR, and logical NOT, respectively, | ||
| - | tion, also as in C, and are used for combining more primitive pattern expressions. | ||
| - | to change the order of evaluation. | ||
| - | |||
| - | The ?: operator is like the same operator in C. If the first pattern is true then the pattern used for testing | ||
| - | | ||
| - | |||
| - | The pattern1, pattern2 form of an expression is called a range pattern. | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | a dash. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | r{n} | ||
| - | r{n,} | ||
| - | | ||
| - | regular | ||
| - | If there is one number followed by a comma, then r is repeated at least n times. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | The escape sequences that are valid in string constants (see String Constants) are also valid in regular expressions. | ||
| - | |||
| - | | ||
| - | | ||
| - | from character set to character set. For example, the notion of what is an alphabetic | ||
| - | | ||
| - | |||
| - | | ||
| - | [:, a keyword denoting the class, and :]. The character classes defined by the POSIX standard are: | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | For example, before the POSIX standard, to match alphanumeric characters, you would have had to write / | ||
| - | | ||
| - | from ASCII, this might not even match the ASCII alphanumeric characters. | ||
| - | / | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | A collating symbol is a multi-character collating element enclosed in [. and .]. For example, if ch is a collating ele‐ | ||
| - | ment, then [[.ch.]] | ||
| - | matches either c or h. | ||
| - | |||
| - | | ||
| - | An equivalence | ||
| - | and =]. For example, the name e might be used to represent all of “e,” “´,” and “`.” | ||
| - | expression that matches any of e, ´, or `. | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | The \y, \B, \<, \>, \s, \S, \w, \W, \`, and \' operators are specific to gawk; they are extensions based on facilities in the GNU | ||
| - | | ||
| - | |||
| - | The various command line options control how gawk interprets characters in regular expressions. | ||
| - | |||
| - | No options | ||
| - | In the default case, gawk provides all the facilities of POSIX regular expressions and the GNU regular expression opera‐ | ||
| - | tors described above. | ||
| - | |||
| - | | ||
| - | Only POSIX regular expressions are supported, the GNU operators are not special. | ||
| - | |||
| - | | ||
| - | Traditional UNIX awk regular expressions are matched. | ||
| - | available. | ||
| - | regular expression metacharacters. | ||
| - | |||
| - | | ||
| - | Allow interval expressions in regular expressions, | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | those in C. | ||
| - | |||
| - | | ||
| - | The operators in AWK, in order of decreasing precedence, are: | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | ++ -- | ||
| - | |||
| - | | ||
| - | |||
| - | + - ! Unary plus, unary minus, and logical negation. | ||
| - | |||
| - | * / % | ||
| - | |||
| - | + - | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | < > <= >= != == | ||
| - | The regular relational operators. | ||
| - | |||
| - | ~ !~ Regular expression match, negated match. | ||
| - | | ||
| - | / | ||
| - | |||
| - | | ||
| - | |||
| - | && | ||
| - | |||
| - | | ||
| - | |||
| - | ?: | ||
| - | sion is expr2, otherwise it is expr3. | ||
| - | |||
| - | = += -= *= /= %= ^= | ||
| - | | ||
| - | |||
| - | | ||
| - | The control statements are as follows: | ||
| - | |||
| - | if (condition) statement [ else statement ] | ||
| - | while (condition) statement | ||
| - | do statement while (condition) | ||
| - | for (expr1; expr2; expr3) statement | ||
| - | for (var in array) statement | ||
| - | break | ||
| - | continue | ||
| - | delete array[index] | ||
| - | delete array | ||
| - | exit [ expression ] | ||
| - | { statements } | ||
| - | switch (expression) { | ||
| - | case value|regex : statement | ||
| - | ... | ||
| - | [ default: statement ] | ||
| - | } | ||
| - | |||
| - | I/O Statements | ||
| - | The input/ | ||
| - | |||
| - | | ||
| - | pipe to a co-process. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | Run command piping the output either into $0 or var, as above, and RT. | ||
| - | |||
| - | | ||
| - | Run command as a co-process piping the output either into $0 or var, as above, and RT. Co-processes are a | ||
| - | gawk extension. | ||
| - | |||
| - | | ||
| - | the first pattern in the AWK program. | ||
| - | | ||
| - | |||
| - | | ||
| - | NAME and ARGIND are updated, FNR is reset to 1, and processing starts over with the first pattern | ||
| - | AWK program. | ||
| - | |||
| - | | ||
| - | |||
| - | print expr-list | ||
| - | the value of ORS. | ||
| - | |||
| - | print expr-list >file Print expressions on file. Each expression is separated by the value of OFS. The output record is termi‐ | ||
| - | nated with the value of ORS. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | null string, then flush all open output files and pipes. | ||
| - | |||
| - | | ||
| - | |||
| - | print ... >> file | ||
| - | Appends output to the file. | ||
| - | |||
| - | print ... | command | ||
| - | Writes on a pipe. | ||
| - | |||
| - | print ... |& command | ||
| - | Sends data to a co-process or socket. | ||
| - | |||
| - | The getline command returns 1 on success, 0 on end of file, and -1 on an error. | ||
| - | ing the problem. | ||
| - | |||
| - | NOTE: Failure in opening a two-way socket results in a non-fatal error being returned to the calling function. If using a pipe, | ||
| - | | ||
| - | mand or socket. | ||
| - | |||
| - | The printf Statement | ||
| - | The AWK versions of the printf statement and sprintf() function (see below) accept the following | ||
| - | mats: | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | %d, %i A decimal number (the integer part). | ||
| - | |||
| - | %e, %E A floating point number of the form [-]d.dddddde[+-]dd. | ||
| - | |||
| - | %f, %F A floating point number of the form [-]ddd.dddddd. | ||
| - | like %f, but uses capital letters for special “not a number” and “infinity” values. If %F is not available, gawk uses %f. | ||
| - | |||
| - | %g, %G Use %e or %f conversion, whichever is shorter, with nonsignificant zeros suppressed. | ||
| - | %e. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | %x, %X An unsigned hexadecimal number (an integer). | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | ily for use in translated versions of format strings, not in the original text of an AWK program. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | the data to be formatted is positive. | ||
| - | |||
| - | # | ||
| - | or 0X for a nonzero result. | ||
| - | zeros are not removed from the result. | ||
| - | |||
| - | | ||
| - | only to the numeric | ||
| - | printed. | ||
| - | |||
| - | ' | ||
| - | also use the locale' | ||
| - | library and in the definition of the current locale. | ||
| - | |||
| - | | ||
| - | zeroes. | ||
| - | |||
| - | | ||
| - | of digits you want printed to the right of the decimal point. | ||
| - | of significant | ||
| - | For %s, it specifies the maximum number of characters from the string that should be printed. | ||
| - | |||
| - | The dynamic width and prec capabilities of the ISO C printf() routines are supported. | ||
| - | | ||
| - | a dynamic width or precision, supply the count$ after the * in the format string. | ||
| - | |||
| - | | ||
| - | When doing I/O redirection from either print or printf into a file, or via getline from a file, gawk recognizes | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | / | ||
| - | |||
| - | / | ||
| - | |||
| - | / | ||
| - | |||
| - | / | ||
| - | |||
| - | These are particularly useful for error messages. | ||
| - | |||
| - | print "You blew it!" > "/ | ||
| - | |||
| - | | ||
| - | |||
| - | print "You blew it!" | "cat 1>& | ||
| - | |||
| - | The following special filenames may be used with the |& co-process operator for creating TCP/IP network connections: | ||
| - | |||
| - | / | ||
| - | / | ||
| - | / | ||
| - | Files for a TCP/IP connection on local port lport to remote host rhost on remote port rport. | ||
| - | system pick a port. Use /inet4 to force an IPv4 connection, and /inet6 to force an IPv6 connection. | ||
| - | system default (most likely IPv4). | ||
| - | |||
| - | / | ||
| - | / | ||
| - | / | ||
| - | Similar, but use UDP/IP instead of TCP/IP. | ||
| - | |||
| - | | ||
| - | AWK has the following built-in arithmetic functions: | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | Gawk has the following built-in string functions: | ||
| - | |||
| - | | ||
| - | for comparing values, and replace the indices of the sorted values s with sequential | ||
| - | | ||
| - | | ||
| - | | ||
| - | can also be the name of a user-defined comparison function as described in PROCINFO[" | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | same as described in asort() above. | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | not changed. | ||
| - | |||
| - | | ||
| - | | ||
| - | with the text that was actually matched. | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | sion, with an array argument, length() returns the number of elements in the array. | ||
| - | |||
| - | | ||
| - | | ||
| - | If array a is provided, a is cleared and then elements 1 through n are filled with the portions of s that | ||
| - | | ||
| - | s matched by the entire regular expression r. Subscripts a[n, " | ||
| - | | ||
| - | |||
| - | | ||
| - | Split the string s into the array a and the separators array seps on the regular expression r, and return | ||
| - | the number of fields. | ||
| - | | ||
| - | seps are cleared first. | ||
| - | |||
| - | | ||
| - | Split the string s into the array a and the separators array seps on the regular expression r, and return | ||
| - | | ||
| - | | ||
| - | ing whitespace in s goes into the extra array element seps[0] and trailing whitespace goes into the extra | ||
| - | array element seps[n], where n is the return value of split(s, a, r, seps). | ||
| - | cally to field splitting, described above. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | If str begins with a leading 0x or 0X, treat it as a hexadecimal number. | ||
| - | mal number. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | ing lowercase counterparts. | ||
| - | |||
| - | | ||
| - | ing uppercase counterparts. | ||
| - | |||
| - | Gawk is multibyte aware. | ||
| - | |||
| - | Time Functions | ||
| - | Since one of the primary uses of AWK programs is processing log files that contain time stamp information, | ||
| - | | ||
| - | |||
| - | | ||
| - | Turn datespec into a time stamp of the same form as returned by systime(), and return the result. | ||
| - | | ||
| - | | ||
| - | | ||
| - | these numbers need not be within the ranges specified; for example, an hour of -1 means 1 hour before | ||
| - | | ||
| - | | ||
| - | | ||
| - | mine whether daylight saving time is in effect for the specified time. If datespec does not contain enough elements or | ||
| - | if the resulting time is out of range, mktime() returns -1. | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | to the output of date(1) is used. The default format is available in PROCINFO[" | ||
| - | the strftime() function in ISO C for the format conversions that are guaranteed to be available. | ||
| - | |||
| - | | ||
| - | |||
| - | Bit Manipulations Functions | ||
| - | | ||
| - | max_t integers, doing the operation, and then converting the result back to floating point. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | Type Function | ||
| - | The following function is for use with multidimensional arrays. | ||
| - | |||
| - | | ||
| - | Return true if x is an array, false otherwise. | ||
| - | |||
| - | | ||
| - | The following functions may be used from within your AWK program for translating strings at run-time. | ||
| - | GAWK: Effective AWK Programming. | ||
| - | |||
| - | | ||
| - | Specify | ||
| - | locations (e.g., during testing). | ||
| - | The default domain is the value of TEXTDOMAIN. | ||
| - | current binding for the given domain. | ||
| - | |||
| - | | ||
| - | Return | ||
| - | current value of TEXTDOMAIN. | ||
| - | If you supply a value for category, it must be a string equal to one of the known locale | ||
| - | Effective AWK Programming. | ||
| - | |||
| - | | ||
| - | Return the plural form used for number of the translation of string1 and string2 in text domain domain for locale category | ||
| - | category. | ||
| - | SAGES" | ||
| - | If you supply | ||
| - | Effective AWK Programming. | ||
| - | |||
| - | USER-DEFINED FUNCTIONS | ||
| - | | ||
| - | |||
| - | function name(parameter list) { statements } | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | Since functions were not originally part of the AWK language, the provision for local variables | ||
| - | | ||
| - | | ||
| - | |||
| - | function | ||
| - | { | ||
| - | ... | ||
| - | } | ||
| - | |||
| - | /abc/ { ... ; f(1, 2) ; ... } | ||
| - | |||
| - | The left parenthesis in a function call is required to immediately follow the function name, without any intervening | ||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | As a gawk extension, functions may be called indirectly. To do this, assign the name of the function to be called, as a string, | ||
| - | to a variable. | ||
| - | function | ||
| - | { | ||
| - | print " | ||
| - | ... | ||
| - | } | ||
| - | |||
| - | { ... | ||
| - | | ||
| - | | ||
| - | ... | ||
| - | } | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | The word func may be used in place of function, although this is deprecated. | ||
| - | |||
| - | DYNAMICALLY LOADING NEW FUNCTIONS | ||
| - | You can dynamically add new built-in functions to the running gawk interpreter with the @load statement. | ||
| - | | ||
| - | |||
| - | SIGNALS | ||
| - | The gawk profiler accepts two signals. | ||
| - | | ||
| - | the profile and function call stack and then exit. | ||
| - | |||
| - | INTERNATIONALIZATION | ||
| - | | ||
| - | mark strings in the AWK program as requiring translation to the local natural language. Such strings are marked in the AWK pro‐ | ||
| - | gram with a leading underscore (“_”). | ||
| - | |||
| - | gawk 'BEGIN { print " | ||
| - | |||
| - | | ||
| - | |||
| - | gawk 'BEGIN { print _" | ||
| - | |||
| - | might print bonjour, monde in France. | ||
| - | |||
| - | There are several steps involved in producing and running a localizable AWK program. | ||
| - | |||
| - | | ||
| - | gram: | ||
| - | |||
| - | BEGIN { TEXTDOMAIN = " | ||
| - | |||
| - | This allows gawk to find the .gmo file associated with your program. | ||
| - | which likely does not contain translations for your program. | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | The internationalization features are described in full detail in GAWK: Effective AWK Programming. | ||
| - | |||
| - | POSIX COMPATIBILITY | ||
| - | | ||
| - | To this end, gawk incorporates the following user visible features which are not described in the AWK book, but are part of the | ||
| - | Brian Kernighan' | ||
| - | |||
| - | | ||
| - | after the BEGIN rule is executed. | ||
| - | | ||
| - | to match its documentation, | ||
| - | that depended upon the old behavior. | ||
| - | |||
| - | | ||
| - | about but otherwise ignores undefined options. | ||
| - | | ||
| - | |||
| - | The AWK book does not define the return value of srand(). | ||
| - | ing track of random number sequences. | ||
| - | |||
| - | Other new features are: The use of multiple -f options (from MKS awk); the ENVIRON array; the \a, and \v escape | ||
| - | | ||
| - | Bell Laboratories version); and the ISO C conversion specifications in printf (done first in the Bell Laboratories version). | ||
| - | |||
| - | HISTORICAL FEATURES | ||
| - | There is one feature of historical AWK implementations that gawk supports: It is possible to call the length() built-in | ||
| - | not only with no argument, but even without parentheses! | ||
| - | |||
| - | a = length | ||
| - | |||
| - | is the same as either of | ||
| - | |||
| - | a = length() | ||
| - | a = length($0) | ||
| - | |||
| - | Using this feature is poor practice, and gawk issues a warning about its use if --lint is specified on the command line. | ||
| - | |||
| - | GNU EXTENSIONS | ||
| - | | ||
| - | can be disabled by invoking gawk with the --traditional or --posix options. | ||
| - | |||
| - | The following features of gawk are not available in POSIX awk. | ||
| - | |||
| - | · No path search is performed for files named via the -f option. | ||
| - | |||
| - | · There is no facility for doing file inclusion (gawk' | ||
| - | |||
| - | · There is no facility for dynamically adding new functions written in C (gawk' | ||
| - | |||
| - | · The \x escape sequence. | ||
| - | |||
| - | · The ability to continue lines after ? and :. (Disabled with --posix.) | ||
| - | |||
| - | · Octal and hexadecimal constants in AWK programs. | ||
| - | |||
| - | · The ARGIND, BINMODE, ERRNO, LINT, RT and TEXTDOMAIN variables are not special. | ||
| - | |||
| - | · The IGNORECASE variable and its side-effects are not available. | ||
| - | |||
| - | · The FIELDWIDTHS variable and fixed-width field splitting. | ||
| - | |||
| - | · The FPAT variable and field splitting based on field values. | ||
| - | |||
| - | · The PROCINFO array is not available. | ||
| - | |||
| - | · The use of RS as a regular expression. | ||
| - | |||
| - | · The special file names available for I/O redirection are not recognized. | ||
| - | |||
| - | · The |& operator for creating co-processes. | ||
| - | |||
| - | · The BEGINFILE and ENDFILE special patterns are not available. | ||
| - | |||
| - | · The ability to split out individual characters using the null string as the value of FS, and as the third argument to split(). | ||
| - | |||
| - | · An optional fourth argument to split() to receive the separator texts. | ||
| - | |||
| - | · The optional second argument to the close() function. | ||
| - | |||
| - | · The optional third argument to the match() function. | ||
| - | |||
| - | · The ability to use positional specifiers with printf and sprintf(). | ||
| - | |||
| - | · The ability to pass an array to length(). | ||
| - | |||
| - | · The and(), asort(), asorti(), bindtextdomain(), | ||
| - | | ||
| - | |||
| - | · Localizable strings. | ||
| - | |||
| - | | ||
| - | | ||
| - | The return value is -1 if the named file, pipe or co-process was not opened with a redirection. | ||
| - | |||
| - | When gawk is invoked with the --traditional option, if the fs argument to the -F option is “t”, then FS is set to the tab charac‐ | ||
| - | | ||
| - | this is a rather ugly special case, it is not the default behavior. | ||
| - | | ||
| - | |||
| - | ENVIRONMENT VARIABLES | ||
| - | The AWKPATH environment variable can be used to provide a list of directories that gawk searches when looking for files named via | ||
| - | | ||
| - | | ||
| - | |||
| - | The AWKLIBPATH environment variable can be used to provide a list of directories that gawk searches when looking for files named | ||
| - | via the -l and --load options. | ||
| - | |||
| - | | ||
| - | pipe or two-way communication including sockets. | ||
| - | |||
| - | For connection to a remote host via socket, GAWK_SOCK_RETRIES controls the number of retries, and GAWK_MSEC_SLEEP and the inter‐ | ||
| - | | ||
| - | | ||
| - | |||
| - | If POSIXLY_CORRECT exists in the environment, | ||
| - | | ||
| - | |||
| - | EXIT STATUS | ||
| - | If the exit statement is used with a value, then gawk exits with the numeric value given to it. | ||
| - | |||
| - | | ||
| - | zero. | ||
| - | |||
| - | If an error occurs, gawk exits with the value of the C constant EXIT_FAILURE. | ||
| - | |||
| - | If gawk exits because of a fatal error, the exit status is 2. On non-POSIX systems, this value may be mapped to EXIT_FAILURE. | ||
| - | |||
| - | VERSION INFORMATION | ||
| - | This man page documents gawk, version 4.1. | ||
| - | |||
| - | AUTHORS | ||
| - | The original version of UNIX awk was designed and implemented by Alfred Aho, Peter Weinberger, and Brian Kernighan of Bell Labo‐ | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | See GAWK: Effective AWK Programming for a full list of the contributors to gawk and its documentation. | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | BUG REPORTS | ||
| - | If you find a bug in gawk, please send electronic mail to bug-gawk@gnu.org. | ||
| - | sion, the version of gawk (from gawk --version), which C compiler you used to compile it, and a test program and data that are as | ||
| - | small as possible for reproducing the problem. | ||
| - | |||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | what you think is a bug really is, instead of just a quirk in the language. | ||
| - | |||
| - | | ||
| - | bug reports there is an unreliable way to report bugs. Instead, please use the electronic mail addresses given above. | ||
| - | |||
| - | | ||
| - | fine, but please send a copy to the official email address as well, since there' | ||
| - | | ||
| - | |||
| - | BUGS | ||
| - | The -F option is not necessary given the command line variable assignment feature; it remains only for backwards compatibility. | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | SEE ALSO | ||
| - | | ||
| - | |||
| - | The AWK Programming Language, Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger, Addison-Wesley, | ||
| - | |||
| - | GAWK: Effective AWK Programming, | ||
| - | | ||
| - | |||
| - | EXAMPLES | ||
| - | Print and sort the login names of all users: | ||
| - | |||
| - | BEGIN { FS = ":" | ||
| - | { print $1 | " | ||
| - | |||
| - | Count lines in a file: | ||
| - | |||
| - | { nlines++ } | ||
| - | END { print nlines } | ||
| - | |||
| - | | ||
| - | |||
| - | { print FNR, $0 } | ||
| - | |||
| - | | ||
| - | |||
| - | { print NR, $0 } | ||
| - | |||
| - | Run an external command for particular lines of data: | ||
| - | |||
| - | tail -f access_log | | ||
| - | awk '/ | ||
| - | |||
| - | ACKNOWLEDGEMENTS | ||
| - | Brian Kernighan provided valuable assistance during testing and debugging. | ||
| - | |||
| - | COPYING PERMISSIONS | ||
| - | | ||
| - | 2012, 2013, 2014 Free Software Foundation, Inc. | ||
| - | |||
| - | | ||
| - | sion notice are preserved on all copies. | ||
| - | |||
| - | | ||
| - | | ||
| - | |||
| - | | ||
| - | for modified versions, except that this permission notice may be stated in a translation approved by the Foundation. | ||
| - | |||
| - | |||
| - | |||
| - | Free Software Foundation | ||
| - | </ | ||
