home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 6
/
FreshFish_September1994.bin
/
gnu
/
info
/
gawk.info-3
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-09-02
|
50KB
|
909 lines
This is Info file gawk.info, produced by Makeinfo-1.55 from the input
file /gnu/src/amiga/gawk-2.15.5/gawk.texi.
This file documents `awk', a program that you can use to select
particular records in a file and perform operations upon them.
This is Edition 0.15 of `The GAWK Manual',
for the 2.15 version of the GNU implementation
of AWK.
Copyright (C) 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided that
the entire resulting derived work is distributed under the terms of a
permission notice identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that this permission notice may be stated in a
translation approved by the Foundation.
File: gawk.info, Node: Output Separators, Next: OFMT, Prev: Print Examples, Up: Printing
Output Separators
=================
As mentioned previously, a `print' statement contains a list of
items, separated by commas. In the output, the items are normally
separated by single spaces. But they do not have to be spaces; a
single space is only the default. You can specify any string of
characters to use as the "output field separator" by setting the
built-in variable `OFS'. The initial value of this variable is the
string `" "', that is, just a single space.
The output from an entire `print' statement is called an "output
record". Each `print' statement outputs one output record and then
outputs a string called the "output record separator". The built-in
variable `ORS' specifies this string. The initial value of the
variable is the string `"\n"' containing a newline character; thus,
normally each `print' statement makes a separate line.
You can change how output fields and records are separated by
assigning new values to the variables `OFS' and/or `ORS'. The usual
place to do this is in the `BEGIN' rule (*note `BEGIN' and `END'
Special Patterns: BEGIN/END.), so that it happens before any input is
processed. You may also do this with assignments on the command line,
before the names of your input files.
The following example prints the first and second fields of each
input record separated by a semicolon, with a blank line added after
each line:
awk 'BEGIN { OFS = ";"; ORS = "\n\n" }
{ print $1, $2 }' BBS-list
If the value of `ORS' does not contain a newline, all your output
will be run together on a single line, unless you output newlines some
other way.
File: gawk.info, Node: OFMT, Next: Printf, Prev: Output Separators, Up: Printing
Controlling Numeric Output with `print'
=======================================
When you use the `print' statement to print numeric values, `awk'
internally converts the number to a string of characters, and prints
that string. `awk' uses the `sprintf' function to do this conversion.
For now, it suffices to say that the `sprintf' function accepts a
"format specification" that tells it how to format numbers (or
strings), and that there are a number of different ways that numbers
can be formatted. The different format specifications are discussed
more fully in *Note Using `printf' Statements for Fancier Printing:
Printf.
The built-in variable `OFMT' contains the default format
specification that `print' uses with `sprintf' when it wants to convert
a number to a string for printing. By supplying different format
specifications as the value of `OFMT', you can change how `print' will
print your numbers. As a brief example:
awk 'BEGIN { OFMT = "%d" # print numbers as integers
print 17.23 }'
will print `17'.
File: gawk.info, Node: Printf, Next: Redirection, Prev: OFMT, Up: Printing
Using `printf' Statements for Fancier Printing
==============================================
If you want more precise control over the output format than `print'
gives you, use `printf'. With `printf' you can specify the width to
use for each item, and you can specify various stylistic choices for
numbers (such as what radix to use, whether to print an exponent,
whether to print a sign, and how many digits to print after the decimal
point). You do this by specifying a string, called the "format
string", which controls how and where to print the other arguments.
* Menu:
* Basic Printf:: Syntax of the `printf' statement.
* Control Letters:: Format-control letters.
* Format Modifiers:: Format-specification modifiers.
* Printf Examples:: Several examples.
File: gawk.info, Node: Basic Printf, Next: Control Letters, Prev: Printf, Up: Printf
Introduction to the `printf' Statement
--------------------------------------
The `printf' statement looks like this:
printf FORMAT, ITEM1, ITEM2, ...
The entire list of arguments may optionally be enclosed in parentheses.
The parentheses are necessary if any of the item expressions uses a
relational operator; otherwise it could be confused with a redirection
(*note Redirecting Output of `print' and `printf': Redirection.). The
relational operators are `==', `!=', `<', `>', `>=', `<=', `~' and `!~'
(*note Comparison Expressions: Comparison Ops.).
The difference between `printf' and `print' is the argument FORMAT.
This is an expression whose value is taken as a string; it specifies
how to output each of the other arguments. It is called the "format
string".
The format string is the same as in the ANSI C library function
`printf'. Most of FORMAT is text to be output verbatim. Scattered
among this text are "format specifiers", one per item. Each format
specifier says to output the next item at that place in the format.
The `printf' statement does not automatically append a newline to its
output. It outputs only what the format specifies. So if you want a
newline, you must include one in the format. The output separator
variables `OFS' and `ORS' have no effect on `printf' statements.
File: gawk.info, Node: Control Letters, Next: Format Modifiers, Prev: Basic Printf, Up: Printf
Format-Control Letters
----------------------
A format specifier starts with the character `%' and ends with a
"format-control letter"; it tells the `printf' statement how to output
one item. (If you actually want to output a `%', write `%%'.) The
format-control letter specifies what kind of value to print. The rest
of the format specifier is made up of optional "modifiers" which are
parameters such as the field width to use.
Here is a list of the format-control letters:
This prints a number as an ASCII character. Thus, `printf "%c",
65' outputs the letter `A'. The output for a string value is the
first character of the string.
This prints a decimal integer.
This also prints a decimal integer.
This prints a number in scientific (exponential) notation. For
example,
printf "%4.3e", 1950
prints `1.950e+03', with a total of four significant figures of
which three follow the decimal point. The `4.3' are "modifiers",
discussed below.
This prints a number in floating point notation.
This prints a number in either scientific notation or floating
point notation, whichever uses fewer characters.
This prints an unsigned octal integer.
This prints a string.
This prints an unsigned hexadecimal integer.
This prints an unsigned hexadecimal integer. However, for the
values 10 through 15, it uses the letters `A' through `F' instead
of `a' through `f'.
This isn't really a format-control letter, but it does have a
meaning when used after a `%': the sequence `%%' outputs one `%'.
It does not consume an argument.
File: gawk.info, Node: Format Modifiers, Next: Printf Examples, Prev: Control Letters, Up: Printf
Modifiers for `printf' Formats
------------------------------
A format specification can