awk:awk_variables
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
awk:awk_variables [2020/05/06 18:31] – peter | awk:awk_variables [2021/01/06 15:55] (current) – [FNR] peter | ||
---|---|---|---|
Line 13: | Line 13: | ||
|BINMODE|Specifies use of “binary” mode for all file I/O.| | |BINMODE|Specifies use of “binary” mode for all file I/O.| | ||
|CONVFMT|The conversion format for numbers, " | |CONVFMT|The conversion format for numbers, " | ||
+ | |ENVIRON|Environment Variables.| | ||
|FIELDWIDTHS|A white-space separated list of field widths. When set the input is parsed into fields of fixed width, instead of using the value of the FS variable as the field separator.| | |FIELDWIDTHS|A white-space separated list of field widths. When set the input is parsed into fields of fixed width, instead of using the value of the FS variable as the field separator.| | ||
+ | |FILENAME|Current filename.| | ||
|FNR|The input record number in the current input file.| | |FNR|The input record number in the current input file.| | ||
|FPAT|A regular expression describing the contents of the fields in a record. When set the input is parsed into fields, where the fields match the regular expression, instead of using the value of the FS variable as the field separator.| | |FPAT|A regular expression describing the contents of the fields in a record. When set the input is parsed into fields, where the fields match the regular expression, instead of using the value of the FS variable as the field separator.| | ||
|FS|The input field separator, a space by default.| | |FS|The input field separator, a space by default.| | ||
|IGNORECASE|Controls the case-sensitivity of all regular expression and string operations.| | |IGNORECASE|Controls the case-sensitivity of all regular expression and string operations.| | ||
+ | |LINT|Provides dynamic control of the –lint option.| | ||
|NF|The number of fields in the current input record.| | |NF|The number of fields in the current input record.| | ||
|NR|The total number of input records seen so far.| | |NR|The total number of input records seen so far.| | ||
Line 23: | Line 26: | ||
|OFS|The output field separator, a space by default.| | |OFS|The output field separator, a space by default.| | ||
|ORS|The output record separator, by default a newline.| | |ORS|The output record separator, by default a newline.| | ||
+ | |PROCINFO|Information about the process.| | ||
+ | |RLENGTH|The length of the string matched by the match function.| | ||
+ | |RSTART|The first position in the string matched by match function.| | ||
|RS|The input record separator, by default a newline.| | |RS|The input record separator, by default a newline.| | ||
|RT|The record terminator.| | |RT|The record terminator.| | ||
|SUBSEP|The character used to separate multiple subscripts in array elements, by default " | |SUBSEP|The character used to separate multiple subscripts in array elements, by default " | ||
+ | |TEXTDOMAIN|The text domain of the AWK program. | ||
+ | |||
---- | ---- | ||
Line 54: | Line 62: | ||
It represents the index in ARGV of the current file being processed. | It represents the index in ARGV of the current file being processed. | ||
- | < | + | < |
awk ' | awk ' | ||
print " | print " | ||
Line 95: | Line 103: | ||
ARGV[3] = three | ARGV[3] = three | ||
</ | </ | ||
+ | ---- | ||
+ | |||
+ | ===== BINMODE ===== | ||
+ | |||
+ | It is used to specify binary mode for all file I/O on non-POSIX systems. | ||
+ | |||
+ | Numeric values of 1, 2, or 3 specify that input files, output files, or all files, respectively, | ||
+ | |||
+ | String values of **r** or **w** specify that input files or output files, respectively, | ||
+ | |||
+ | String values of **rw** or **wr** specify that all files should use binary I/O. | ||
---- | ---- | ||
Line 104: | Line 123: | ||
Its default value is %.6g. | Its default value is %.6g. | ||
- | < | + | < |
awk 'BEGIN { print " | awk 'BEGIN { print " | ||
</ | </ | ||
Line 133: | Line 152: | ||
**NOTE: | **NOTE: | ||
</ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== ERRNO ===== | ||
+ | |||
+ | A string indicates an error when a redirection fails for getline or if a close call fails. | ||
+ | |||
+ | <code awk> | ||
+ | awk 'BEGIN { ret = getline < " | ||
+ | </ | ||
+ | |||
+ | returns: | ||
+ | |||
+ | <code bash> | ||
+ | Error: No such file or directory | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== FIELDWIDTHS ===== | ||
+ | |||
+ | A space separated list of field widths variable is set, GAWK parses the input into fields of fixed width, instead of using the value of the FS variable as the field separator. | ||
---- | ---- | ||
Line 152: | Line 193: | ||
<WRAP info> | <WRAP info> | ||
**NOTE:** Please note that FILENAME is undefined in the BEGIN block. | **NOTE:** Please note that FILENAME is undefined in the BEGIN block. | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== FNR ===== | ||
+ | |||
+ | It is similar to NR, but relative to the current file. | ||
+ | |||
+ | It is useful when AWK is operating on multiple files. | ||
+ | |||
+ | It represents the number of the current record in the current file. | ||
+ | |||
+ | <code bash> | ||
+ | echo -e "One Two\nOne Two Three\nOne Two Three Four" | awk 'FNR < 3' | ||
+ | </ | ||
+ | |||
+ | returns: | ||
+ | |||
+ | < | ||
+ | One Two | ||
+ | One Two Three | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
</ | </ | ||
Line 171: | Line 237: | ||
FS = $ | FS = $ | ||
</ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== IGNORECASE ===== | ||
+ | |||
+ | When this variable is set, GAWK becomes case-insensitive. | ||
+ | |||
+ | <code awk> | ||
+ | awk ' | ||
+ | </ | ||
+ | |||
+ | returns: | ||
+ | |||
+ | <code bash> | ||
+ | 10 | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | <code bash> | ||
+ | awk ' | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== LINT ===== | ||
+ | |||
+ | It provides dynamic control of the **--lint** option from the GAWK program. | ||
+ | |||
+ | When this variable is set, GAWK prints lint warnings. | ||
+ | |||
+ | When assigned the string value fatal, lint warnings become fatal errors, exactly like **--lint=fatal**. | ||
+ | |||
+ | <code awk> | ||
+ | awk 'BEGIN {LINT = 1; a}' | ||
+ | </ | ||
+ | |||
+ | returns: | ||
+ | |||
+ | <code bash> | ||
+ | awk: cmd. line:1: warning: reference to uninitialized variable `a' | ||
+ | awk: cmd. line:1: warning: statement has no effect | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | </ | ||
---- | ---- | ||
Line 209: | Line 325: | ||
One Two Three | One Two Three | ||
</ | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== FNR ===== | ||
- | |||
- | It is similar to NR, but relative to the current file. | ||
- | |||
- | It is useful when AWK is operating on multiple files. | ||
- | |||
- | Value of FNR resets with a new file. | ||
---- | ---- | ||
Line 226: | Line 332: | ||
It represents the output format number and its default value is %.6g. | It represents the output format number and its default value is %.6g. | ||
- | < | + | < |
awk 'BEGIN {print "OFMT = " OFMT}' | awk 'BEGIN {print "OFMT = " OFMT}' | ||
</ | </ | ||
Line 242: | Line 348: | ||
It represents the output field separator and its default value is space. | It represents the output field separator and its default value is space. | ||
- | < | + | < |
awk 'BEGIN {print "OFS = " OFS}' | cat -vte | awk 'BEGIN {print "OFS = " OFS}' | cat -vte | ||
</ | </ | ||
Line 258: | Line 364: | ||
It represents the output record separator and its default value is newline. | It represents the output record separator and its default value is newline. | ||
- | < | + | < |
awk 'BEGIN {print "ORS = " ORS}' | cat -vte | awk 'BEGIN {print "ORS = " ORS}' | cat -vte | ||
</ | </ | ||
Line 267: | Line 373: | ||
ORS = $ | ORS = $ | ||
$ | $ | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== PROCINFO ===== | ||
+ | |||
+ | This is an associative array containing information about the process, such as real and effective UID numbers, process ID number, and so on. | ||
+ | |||
+ | <code awk> | ||
+ | awk 'BEGIN { print PROCINFO[" | ||
+ | </ | ||
+ | |||
+ | returns: | ||
+ | |||
+ | <code bash> | ||
+ | 30510 | ||
</ | </ | ||
Line 277: | Line 399: | ||
AWK's match function searches for a given string in the input-string. | AWK's match function searches for a given string in the input-string. | ||
- | < | + | < |
awk 'BEGIN { if (match(" | awk 'BEGIN { if (match(" | ||
</ | </ | ||
Line 294: | Line 416: | ||
It represents (input) record separator and its default value is newline. | It represents (input) record separator and its default value is newline. | ||
- | < | + | < |
awk 'BEGIN {print "RS = " RS}' | cat -vte | awk 'BEGIN {print "RS = " RS}' | cat -vte | ||
</ | </ | ||
Line 311: | Line 433: | ||
It represents the first position in the string matched by match function. | It represents the first position in the string matched by match function. | ||
- | < | + | < |
awk 'BEGIN { if (match(" | awk 'BEGIN { if (match(" | ||
</ | </ | ||
Line 327: | Line 449: | ||
It represents the separator character for array subscripts and its default value is \034. | It represents the separator character for array subscripts and its default value is \034. | ||
- | < | + | < |
awk 'BEGIN { print " | awk 'BEGIN { print " | ||
</ | </ | ||
Line 343: | Line 465: | ||
It represents the entire input record. | It represents the entire input record. | ||
- | < | + | < |
awk ' | awk ' | ||
</ | </ | ||
Line 357: | Line 479: | ||
48 | 48 | ||
</ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== TEXTDOMAIN ===== | ||
+ | |||
+ | It represents the text domain of the AWK program. | ||
+ | |||
+ | It is used to find the localized translations for the program' | ||
+ | |||
+ | <code awk> | ||
+ | awk 'BEGIN { print TEXTDOMAIN }' | ||
+ | </ | ||
+ | |||
+ | returns: | ||
+ | |||
+ | <code bash> | ||
+ | messages | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | </ | ||
+ | |||
---- | ---- | ||
Line 364: | Line 509: | ||
It represents the nth field in the current record where the fields are separated by FS. | It represents the nth field in the current record where the fields are separated by FS. | ||
- | < | + | < |
awk ' | awk ' | ||
</ | </ |
awk/awk_variables.1588789903.txt.gz · Last modified: 2020/07/15 09:30 (external edit)