|
awk also provides a number of built-in
string variables: - FILENAME
- Contains the name of the current input file. For example, when
running programs against the hobbies file, the value of FILENAME would be hobbies (if that is the file you
are using). If the input is coming from the awk standard input, the value is -.
- FS
- Is the field separator string, giving the character that
is used to separate fields in the current file. The default value
for FS is "" (a single blank), which
as a special case matches both blank and tab. However, if the command
line contains an –F option specifying a
different field separator, FS is a string containing
the given separator character. A program may also assign values to FS to indicate new field separator characters. For example,
you could create a data file with a first line that provides the character
used to separate fields in the records in the rest of the file. An awk program could then contain the instruction:
FNR == 1 { FS = $0 }
This says that the field
separator string FS should be assigned the contents
of the first record in the current data file. The character in this
line is then taken to be the field separator for the rest of the file
(unless FS changes value again). Any FS value of more than one character is used as a regular expression.
For details, see the Input topic of the awk command description in z/OS UNIX System Services Command Reference.
- RS
- Is the input record separator. Just as FS indicates the character that separates fields within records, RS indicates the character that separates one record from
another. By default, RS contains a newline character,
which means that input records are separated by newlines. However,
you can assign a different character to RS; for example,
with:
RS = ";"
input records are separated
by semicolons. This lets you have several records on a single line,
or a single record that extends over several lines. Records are separated
by a semicolon, not a <newline> character. As an important special
case: RS = ""
separates records by empty lines.
- OFS
- Gives the output field separator string. When you use
the print action to print several values,
as in:
{ print A, B, C }
awk prints the output field separator string between each of
the values. By default, OFS contains a single blank
character, which is why output values are separated by a single blank.
However, if you make the assignment: OFS = " : "
the output values are separated by the given string. You can also
use OFS to reconstruct the $0 field
during field assignment.
- ORS
- Gives the output record separator. When you use the print action to print records, awk prints the output record separator at the end of each record.
By default, ORS is the newline character, which is
why print prints a new output line each
time it is called. However, you can use a different separator string
by assigning the string to ORS.
- OFMT
- Is the default output format for numbers when they are
displayed by print. This is a format string
like the one used by printf. By default,
it is %.6g, indicating that numbers are to be displayed
with a maximum of six digits after the decimal point. By changing
OFMT, you can obtain more or less displayed precision.
- CONVFMT
- Is the default format which awk uses when converting numbers into strings internally. This differs
from the OFMT variable, which is used only
when displaying numbers. The internal conversion of a number to a
string occurs when you perform concatenation, indexing, and some comparison
operations. awk converts floating-point
numbers (numbers that are not integers) to strings as if you had specified
the operation:
sprintf(CONVFMT, number ...)
By default, the value of CONVFMT is %.6g. Note: CONVFMT is a POSIX extension not found in traditional implementations
of awk.
|