home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / info / gawk.info-3 (.txt) < prev    next >
GNU Info File  |  1994-12-22  |  50KB  |  909 lines

  1. This is Info file gawk.info, produced by Makeinfo-1.55 from the input
  2. file /gnu/src/amiga/gawk-2.15.5/gawk.texi.
  3.    This file documents `awk', a program that you can use to select
  4. particular records in a file and perform operations upon them.
  5.    This is Edition 0.15 of `The GAWK Manual',
  6. for the 2.15 version of the GNU implementation
  7. of AWK.
  8.    Copyright (C) 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that
  14. the entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that this permission notice may be stated in a
  19. translation approved by the Foundation.
  20. File: gawk.info,  Node: Output Separators,  Next: OFMT,  Prev: Print Examples,  Up: Printing
  21. Output Separators
  22. =================
  23.    As mentioned previously, a `print' statement contains a list of
  24. items, separated by commas.  In the output, the items are normally
  25. separated by single spaces.  But they do not have to be spaces; a
  26. single space is only the default.  You can specify any string of
  27. characters to use as the "output field separator" by setting the
  28. built-in variable `OFS'.  The initial value of this variable is the
  29. string `" "', that is, just a single space.
  30.    The output from an entire `print' statement is called an "output
  31. record".  Each `print' statement outputs one output record and then
  32. outputs a string called the "output record separator".  The built-in
  33. variable `ORS' specifies this string.  The initial value of the
  34. variable is the string `"\n"' containing a newline character; thus,
  35. normally each `print' statement makes a separate line.
  36.    You can change how output fields and records are separated by
  37. assigning new values to the variables `OFS' and/or `ORS'.  The usual
  38. place to do this is in the `BEGIN' rule (*note `BEGIN' and `END'
  39. Special Patterns: BEGIN/END.), so that it happens before any input is
  40. processed.  You may also do this with assignments on the command line,
  41. before the names of your input files.
  42.    The following example prints the first and second fields of each
  43. input record separated by a semicolon, with a blank line added after
  44. each line:
  45.      awk 'BEGIN { OFS = ";"; ORS = "\n\n" }
  46.                 { print $1, $2 }'  BBS-list
  47.    If the value of `ORS' does not contain a newline, all your output
  48. will be run together on a single line, unless you output newlines some
  49. other way.
  50. File: gawk.info,  Node: OFMT,  Next: Printf,  Prev: Output Separators,  Up: Printing
  51. Controlling Numeric Output with `print'
  52. =======================================
  53.    When you use the `print' statement to print numeric values, `awk'
  54. internally converts the number to a string of characters, and prints
  55. that string.  `awk' uses the `sprintf' function to do this conversion.
  56. For now, it suffices to say that the `sprintf' function accepts a
  57. "format specification" that tells it how to format numbers (or
  58. strings), and that there are a number of different ways that numbers
  59. can be formatted.  The different format specifications are discussed
  60. more fully in *Note Using `printf' Statements for Fancier Printing:
  61. Printf.
  62.    The built-in variable `OFMT' contains the default format
  63. specification that `print' uses with `sprintf' when it wants to convert
  64. a number to a string for printing.  By supplying different format
  65. specifications as the value of `OFMT', you can change how `print' will
  66. print your numbers.  As a brief example:
  67.      awk 'BEGIN { OFMT = "%d"  # print numbers as integers
  68.                   print 17.23 }'
  69. will print `17'.
  70. File: gawk.info,  Node: Printf,  Next: Redirection,  Prev: OFMT,  Up: Printing
  71. Using `printf' Statements for Fancier Printing
  72. ==============================================
  73.    If you want more precise control over the output format than `print'
  74. gives you, use `printf'.  With `printf' you can specify the width to
  75. use for each item, and you can specify various stylistic choices for
  76. numbers (such as what radix to use, whether to print an exponent,
  77. whether to print a sign, and how many digits to print after the decimal
  78. point).  You do this by specifying a string, called the "format
  79. string", which controls how and where to print the other arguments.
  80. * Menu:
  81. * Basic Printf::                Syntax of the `printf' statement.
  82. * Control Letters::             Format-control letters.
  83. * Format Modifiers::            Format-specification modifiers.
  84. * Printf Examples::             Several examples.
  85. File: gawk.info,  Node: Basic Printf,  Next: Control Letters,  Prev: Printf,  Up: Printf
  86. Introduction to the `printf' Statement
  87. --------------------------------------
  88.    The `printf' statement looks like this:
  89.      printf FORMAT, ITEM1, ITEM2, ...
  90. The entire list of arguments may optionally be enclosed in parentheses.
  91. The parentheses are necessary if any of the item expressions uses a
  92. relational operator; otherwise it could be confused with a redirection
  93. (*note Redirecting Output of `print' and `printf': Redirection.).  The
  94. relational operators are `==', `!=', `<', `>', `>=', `<=', `~' and `!~'
  95. (*note Comparison Expressions: Comparison Ops.).
  96.    The difference between `printf' and `print' is the argument FORMAT.
  97. This is an expression whose value is taken as a string; it specifies
  98. how to output each of the other arguments.  It is called the "format
  99. string".
  100.    The format string is the same as in the ANSI C library function
  101. `printf'.  Most of FORMAT is text to be output verbatim.  Scattered
  102. among this text are "format specifiers", one per item.  Each format
  103. specifier says to output the next item at that place in the format.
  104.    The `printf' statement does not automatically append a newline to its
  105. output.  It outputs only what the format specifies.  So if you want a
  106. newline, you must include one in the format.  The output separator
  107. variables `OFS' and `ORS' have no effect on `printf' statements.
  108. File: gawk.info,  Node: Control Letters,  Next: Format Modifiers,  Prev: Basic Printf,  Up: Printf
  109. Format-Control Letters
  110. ----------------------
  111.    A format specifier starts with the character `%' and ends with a
  112. "format-control letter"; it tells the `printf' statement how to output
  113. one item.  (If you actually want to output a `%', write `%%'.)  The
  114. format-control letter specifies what kind of value to print.  The rest
  115. of the format specifier is made up of optional "modifiers" which are
  116. parameters such as the field width to use.
  117.    Here is a list of the format-control letters:
  118.      This prints a number as an ASCII character.  Thus, `printf "%c",
  119.      65' outputs the letter `A'.  The output for a string value is the
  120.      first character of the string.
  121.      This prints a decimal integer.
  122.      This also prints a decimal integer.
  123.      This prints a number in scientific (exponential) notation.  For
  124.      example,
  125.           printf "%4.3e", 1950
  126.      prints `1.950e+03', with a total of four significant figures of
  127.      which three follow the decimal point.  The `4.3' are "modifiers",
  128.      discussed below.
  129.      This prints a number in floating point notation.
  130.      This prints a number in either scientific notation or floating
  131.      point notation, whichever uses fewer characters.
  132.      This prints an unsigned octal integer.
  133.      This prints a string.
  134.      This prints an unsigned hexadecimal integer.
  135.      This prints an unsigned hexadecimal integer.  However, for the
  136.      values 10 through 15, it uses the letters `A' through `F' instead
  137.      of `a' through `f'.
  138.      This isn't really a format-control letter, but it does have a
  139.      meaning when used after a `%': the sequence `%%' outputs one `%'.
  140.      It does not consume an argument.
  141. File: gawk.info,  Node: Format Modifiers,  Next: Printf Examples,  Prev: Control Letters,  Up: Printf
  142. Modifiers for `printf' Formats
  143. ------------------------------
  144.    A format specification can