home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / info / gawk.info-4 (.txt) < prev    next >
GNU Info File  |  1994-12-22  |  50KB  |  947 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: Actions,  Next: Expressions,  Prev: Patterns,  Up: Top
  21. Overview of Actions
  22. *******************
  23.    An `awk' program or script consists of a series of rules and
  24. function definitions, interspersed.  (Functions are described later.
  25. *Note User-defined Functions: User-defined.)
  26.    A rule contains a pattern and an action, either of which may be
  27. omitted.  The purpose of the "action" is to tell `awk' what to do once
  28. a match for the pattern is found.  Thus, the entire program looks
  29. somewhat like this:
  30.      [PATTERN] [{ ACTION }]
  31.      [PATTERN] [{ ACTION }]
  32.      ...
  33.      function NAME (ARGS) { ... }
  34.      ...
  35.    An action consists of one or more `awk' "statements", enclosed in
  36. curly braces (`{' and `}').  Each statement specifies one thing to be
  37. done.  The statements are separated by newlines or semicolons.
  38.    The curly braces around an action must be used even if the action
  39. contains only one statement, or even if it contains no statements at
  40. all.  However, if you omit the action entirely, omit the curly braces as
  41. well.  (An omitted action is equivalent to `{ print $0 }'.)
  42.    Here are the kinds of statements supported in `awk':
  43.    * Expressions, which can call functions or assign values to variables
  44.      (*note Expressions as Action Statements: Expressions.).  Executing
  45.      this kind of statement simply computes the value of the expression
  46.      and then ignores it.  This is useful when the expression has side
  47.      effects (*note Assignment Expressions: Assignment Ops.).
  48.    * Control statements, which specify the control flow of `awk'
  49.      programs.  The `awk' language gives you C-like constructs (`if',
  50.      `for', `while', and so on) as well as a few special ones (*note
  51.      Control Statements in Actions: Statements.).
  52.    * Compound statements, which consist of one or more statements
  53.      enclosed in curly braces.  A compound statement is used in order
  54.      to put several statements together in the body of an `if',
  55.      `while', `do' or `for' statement.
  56.    * Input control, using the `getline' command (*note Explicit Input
  57.      with `getline': Getline.), and the `next' statement (*note The
  58.      `next' Statement: Next Statement.).
  59.    * Output statements, `print' and `printf'.  *Note Printing Output:
  60.      Printing.
  61.    * Deletion statements, for deleting array elements.  *Note The
  62.      `delete' Statement: Delete.
  63. File: gawk.info,  Node: Expressions,  Next: Statements,  Prev: Actions,  Up: Top
  64. Expressions as Action Statements
  65. ********************************
  66.    Expressions are the basic building block of `awk' actions.  An
  67. expression evaluates to a value, which you can print, test, store in a
  68. variable or pass to a function.  But beyond that, an expression can
  69. assign a new value to a variable or a field, with an assignment
  70. operator.
  71.    An expression can serve as a statement on its own.  Most other kinds
  72. of statements contain one or more expressions which specify data to be
  73. operated on.  As in other languages, expressions in `awk' include
  74. variables, array references, constants, and function calls, as well as
  75. combinations of these with various operators.
  76. * Menu:
  77. * Constants::                   String, numeric, and regexp constants.
  78. * Variables::                   Variables give names to values for later use.
  79. * Arithmetic Ops::              Arithmetic operations (`+', `-', etc.)
  80. * Concatenation::               Concatenating strings.
  81. * Comparison Ops::              Comparison of numbers and strings
  82.                                 with `<', etc.
  83. * Boolean Ops::                 Combining comparison expressions
  84.                                 using boolean operators
  85.                                 `||' ("or"), `&&' ("and") and `!' ("not").
  86. * Assignment Ops::              Changing the value of a variable or a field.
  87. * Increment Ops::               Incrementing the numeric value of a variable.
  88. * Conversion::                  The conversion of strings to numbers
  89.                                 and vice versa.
  90. * Values::                      The whole truth about numbers and strings.
  91. * Conditional Exp::             Conditional expressions select
  92.                                 between two subexpressions under control
  93.                                 of a third subexpression.
  94. * Function Calls::              A function call is an expression.
  95. * Precedence::                  How various operators nest.
  96. File: gawk.info,  Node: Constants,  Next: Variables,  Prev: Expressions,  Up: Expressions
  97. Constant Expressions
  98. ====================
  99.    The simplest type of expression is the "constant", which always has
  100. the same value.  There are three types of constants: numeric constants,
  101. string constants, and regular expression constants.
  102.    A "numeric constant" stands for a number.  This number can be an
  103. integer, a decimal fraction, or a number in scientific (exponential)
  104. notation.  Note that all numeric values are represented within `awk' in
  105. double-precision floating point.  Here are some examples of numeric
  106. constants, which all have the same value:
  107.      105
  108.      1.05e+2
  109.      1050e-1
  110.    A string constant consists of a sequence of characters enclosed in
  111. double-quote marks.  For example:
  112.      "parrot"
  113. represents the string whose contents are `parrot'.  Strings in `gawk'
  114. can be of any length and they can contain all the possible 8-bit ASCII
  115. characters including ASCII NUL.  Other `awk' implementations may have
  116. difficulty with some character codes.
  117.    Some characters cannot be included literally in a string constant.
  118. You represent them instead with "escape sequences", which are character
  119. sequences beginning with a backslash (`\').
  120.    One use of an escape sequence is to include a double-quote character
  121. in a string constant.  Since a plain double-quote would end the string,
  122. you must use `\"' to represent a single double-quote character as a
  123. part of the string.  The backslash character itself is another
  124. character that cannot be included normally; you write `\\' to put one
  125. backslash in the string.  Thus, the string whose contents are the two
  126. characters `"\' must be written `"\"\\"'.
  127.    Another use of backslash is to represent unprintable characters such
  128. as newline.  While there is nothing to stop you from writing most of
  129. these characters directly in a string constant, they may look ugly.
  130.    Here is a table of all the escape sequences used in `awk':
  131.      Represents a literal backslash, `\'.
  132.      Represents the "alert" character, control-g, ASCII code 7.
  133.      Represents a backspace, control-h, ASCII code 8.
  134.      Represents a formfeed, control-l, ASCII code 12.
  135.      Represents a newline, control-j, ASCII code 10.
  136.      Represents a carriage return, control-m, ASCII code 13.
  137.      Represents a horizontal tab, control-i, ASCII code 9.
  138.      Represents a vertical tab, control-k, ASCII code 11.
  139. `\NNN'
  140.      Represents the octal value NNN, where NNN are one to three digits
  141.      between 0 and 7.  For example, the code for the ASCII ESC (escape)
  142.      character is `\033'.
  143. `\xHH...'
  144.      Represents the hexadecimal value HH, where HH are hexadecimal
  145.      digits (`0' through `9' and either `A' through `F' or `a' through
  146.      `f').  Li