home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume22 / gawk2.11 / part07 / gawk.texinfo.06
Encoding:
Text File  |  1990-06-07  |  48.5 KB  |  1,399 lines

  1. the @code{BEGIN} rule was executed.  Some applications came to depend
  2. upon this ``feature''.  When @code{awk} was changed to be more consistent,
  3. the @samp{-v} option was added to accomodate applications that depended
  4. upon this old behaviour.
  5.  
  6. The variable assignment feature is most useful for assigning to variables
  7. such as @code{RS}, @code{OFS}, and @code{ORS}, which control input and
  8. output formats, before scanning the data files.  It is also useful for
  9. controlling state if multiple passes are needed over a data file.  For
  10. example:@refill
  11.  
  12. @cindex multiple passes over data
  13. @cindex passes, multiple
  14. @example
  15. awk 'pass == 1  @{ @var{pass 1 stuff} @}
  16.      pass == 2  @{ @var{pass 2 stuff} @}' pass=1 datafile pass=2 datafile
  17. @end example
  18.  
  19. @node AWKPATH Variable,, Other Arguments, Command Line
  20. @section The @code{AWKPATH} Environment Variable
  21. @cindex @code{AWKPATH} environment variable
  22. @cindex search path
  23. @cindex directory search
  24. @cindex path, search
  25. @c @cindex differences between @code{gawk} and @code{awk}
  26.  
  27. The previous section described how @code{awk} program files can be named
  28. on the command line with the @samp{-f} option.  In some @code{awk}
  29. implementations, you must supply a precise path name for each program
  30. file, unless the file is in the current directory.
  31.  
  32. But in @code{gawk}, if the file name supplied in the @samp{-f} option
  33. does not contain a @samp{/}, then @code{gawk} searches a list of
  34. directories (called the @dfn{search path}), one by one, looking for a
  35. file with the specified name.
  36.  
  37. The search path is actually a string containing directory names
  38. separated by colons.  @code{gawk} gets its search path from the
  39. @code{AWKPATH} environment variable.  If that variable does not exist,
  40. @code{gawk} uses the default path, which is
  41. @samp{.:/usr/lib/awk:/usr/local/lib/awk}.@refill
  42.  
  43. The search path feature is particularly useful for building up libraries
  44. of useful @code{awk} functions.  The library files can be placed in a
  45. standard directory that is in the default path, and then specified on
  46. the command line with a short file name.  Otherwise, the full file name
  47. would have to be typed for each file.
  48.  
  49. Path searching is not done if @code{gawk} is in compatibility mode.
  50. @xref{Command Line}.
  51.  
  52. @strong{Note:} if you want files in the current directory to be found,
  53. you must include the current directory in the path, either by writing
  54. @file{.} as an entry in the path, or by writing a null entry in the
  55. path.  (A null entry is indicated by starting or ending the path with a
  56. colon, or by placing two colons next to each other (@samp{::}).)  If the
  57. current directory is not included in the path, then files cannot be
  58. found in the current directory.  This path search mechanism is identical
  59. to the shell's.
  60. @c someday, @cite{The Bourne Again Shell}....
  61.  
  62. @node Language History, Gawk Summary, Command Line, Top
  63. @chapter The Evolution of the @code{awk} Language
  64.  
  65. This manual describes the GNU implementation of @code{awk}, which is patterned
  66. after the System V Release 4 version.  Many @code{awk} users are only familiar
  67. with the original @code{awk} implementation in Version 7 Unix, which is also
  68. the basis for the version in Berkeley Unix.  This chapter briefly describes
  69. the evolution of the @code{awk} language.
  70.  
  71. @menu
  72. * V7/S5R3.1::   The major changes between V7 and System V Release 3.1.
  73.  
  74. * S5R4::        The minor changes between System V Releases 3.1 and 4.
  75.  
  76. * S5R4/GNU::    The extensions in @code{gawk} not in System V Release 4.
  77. @end menu
  78.  
  79. @node V7/S5R3.1, S5R4, Language History, Language History
  80. @section Major Changes Between V7 and S5R3.1
  81.  
  82. The @code{awk} language evolved considerably between the release of
  83. Version 7 Unix (1978) and the new version first made widely available in
  84. System V Release 3.1 (1987).  This section summarizes the changes, with
  85. cross-references to further details.
  86.  
  87. @itemize @bullet
  88. @item
  89. The requirement for @samp{;} to separate rules on a line
  90. (@pxref{Statements/Lines}).
  91.  
  92. @item
  93. User-defined functions, and the @code{return} statement
  94. (@pxref{User-defined}).
  95.  
  96. @item
  97. The @code{delete} statement (@pxref{Delete}).
  98.  
  99. @item
  100. The @code{do}-@code{while} statement (@pxref{Do Statement}).
  101.  
  102. @item
  103. The built-in functions @code{atan2}, @code{cos}, @code{sin}, @code{rand} and
  104. @code{srand} (@pxref{Numeric Functions}).
  105.  
  106. @item
  107. The built-in functions @code{gsub}, @code{sub}, and @code{match}
  108. (@pxref{String Functions}).
  109.  
  110. @item
  111. The built-in functions @code{close} and @code{system} (@pxref{I/O
  112. Functions}).
  113.  
  114. @item
  115. The @code{ARGC}, @code{ARGV}, @code{FNR}, @code{RLENGTH}, @code{RSTART},
  116. and @code{SUBSEP} built-in variables (@pxref{Built-in Variables}).
  117.  
  118. @item
  119. The conditional expression using the operators @samp{?} and @samp{:}
  120. (@pxref{Conditional Exp}).
  121.  
  122. @item
  123. The exponentiation operator @samp{^} (@pxref{Arithmetic Ops}) and its
  124. assignment operator form @samp{^=} (@pxref{Assignment Ops}).@refill
  125.  
  126. @item
  127. C-compatible operator precedence, which breaks some old @code{awk}
  128. programs (@pxref{Precedence}).
  129.  
  130. @item
  131. Regexps as the value of @code{FS} (@pxref{Field Separators}), or as the
  132. third argument to the @code{split} function (@pxref{String
  133. Functions}).@refill
  134.  
  135. @item
  136. Dynamic regexps as operands of the @samp{~} and @samp{!~} operators
  137. (@pxref{Regexp Usage}).
  138.  
  139. @item
  140. Escape sequences (@pxref{Constants}) in regexps.@refill
  141.  
  142. @item
  143. The escape sequences @samp{\b}, @samp{\f}, and @samp{\r}
  144. (@pxref{Constants}).
  145.  
  146. @item
  147. Redirection of input for the @code{getline} function (@pxref{Getline}).
  148.  
  149. @item
  150. Multiple @code{BEGIN} and @code{END} rules (@pxref{BEGIN/END}).
  151.  
  152. @item
  153. Simulation of multidimensional arrays (@pxref{Multi-dimensional}).
  154. @end itemize
  155.  
  156. @node S5R4, S5R4/GNU, V7/S5R3.1, Language History
  157. @section Minor Changes between S5R3.1 and S5R4
  158.  
  159. The System V Release 4 version of Unix @code{awk} added these features:
  160.  
  161. @itemize @bullet
  162. @item
  163. The @code{ENVIRON} variable (@pxref{Built-in Variables}).
  164.  
  165. @item
  166. Multiple @samp{-f} options on the command line (@pxref{Command Line}).
  167.  
  168. @item
  169. The @samp{-v} option for assigning variables before program execution begins
  170. (@pxref{Command Line}).
  171.  
  172. @item
  173. The @samp{--} option for terminating command line options.
  174.  
  175. @item
  176. The @samp{\a}, @samp{\v}, and @samp{\x} escape sequences (@pxref{Constants}).
  177.  
  178. @item
  179. A defined return value for the @code{srand} built-in function
  180. (@pxref{Numeric Functions}).
  181.  
  182. @item
  183. The @code{toupper} and @code{tolower} built-in string functions
  184. for case translation (@pxref{String Functions}).
  185.  
  186. @item
  187. A cleaner specification for the @samp{%c} format-control letter in the
  188. @code{printf} function (@pxref{Printf}).
  189.  
  190. @item
  191. The use of constant regexps such as @code{/foo/} as expressions, where
  192. they are equivalent to use of the matching operator, as in @code{$0 ~
  193. /foo/}.
  194. @end itemize
  195.  
  196. @node S5R4/GNU, , S5R4, Language History
  197. @section Extensions In @code{gawk} Not In S5R4
  198.  
  199. The GNU implementation, @code{gawk}, adds these features:
  200.  
  201. @itemize @bullet
  202. @item
  203. The @code{AWKPATH} environment variable for specifying a path search for
  204. the @samp{-f} command line option (@pxref{Command Line}).
  205.  
  206. @item
  207. The @samp{-C} and @samp{-V} command line options (@pxref{Command Line}).
  208.  
  209. @item
  210. The @code{IGNORECASE} variable and its effects (@pxref{Case-sensitivity}).
  211.  
  212. @item
  213. The @file{/dev/stdin}, @file{/dev/stdout}, @file{/dev/stderr}, and
  214. @file{/dev/fd/@var{n}} file name interpretation (@pxref{Special Files}).
  215.  
  216. @item
  217. The @samp{-c} option to turn off these extensions (@pxref{Command Line}).
  218.  
  219. @item
  220. The @samp{-a} and @samp{-e} options to specify the syntax of regular
  221. expressions that @code{gawk} will accept (@pxref{Command Line}).
  222. @end itemize
  223.  
  224. @node Gawk Summary, Sample Program, Language History, Top
  225. @appendix @code{gawk} Summary
  226.  
  227. @ignore
  228. See, man pages are good for something.  This chapter started life as the
  229. gawk.1 man page for 2.11.
  230. @end ignore
  231.  
  232. This appendix provides a brief summary of the @code{gawk} command line and the
  233. @code{awk} language.  It is designed to serve as ``quick reference.''  It is
  234. therefore terse, but complete.
  235.  
  236. @menu
  237. * Command Line Summary::  Recapitulation of the command line.
  238. * Language Summary::      A terse review of the language.
  239. * Variables/Fields::      Variables, fields, and arrays.
  240. * Rules Summary::         Patterns and Actions, and their component parts.
  241. * Functions Summary::     Defining and calling functions.
  242. @end menu
  243.  
  244. @node Command Line Summary, Language Summary, Gawk Summary, Gawk Summary
  245. @appendixsec Command Line Options Summary
  246.  
  247. The command line consists of options to @code{gawk} itself, the
  248. @code{awk} program text (if not supplied via the @samp{-f} option), and
  249. values to be made available in the @code{ARGC} and @code{ARGV}
  250. predefined @code{awk} variables:
  251.  
  252. @example
  253. awk @r{[@code{-F@var{fs}}] [@code{-v @var{var}=@var{val}}] [@code{-V}] [@code{-C}] [@code{-c}] [@code{-a}] [@code{-e}] [@code{--}]} '@var{program}' @var{file} @dots{}
  254. awk @r{[@code{-F@var{fs}}] @code{-f @var{source-file}} [@code{-f @var{source-file} @dots{}}] [@code{-v @var{var}=@var{val}}] [@code{-V}] [@code{-C}] [@code{-c}] [@code{-a}] [@code{-e}] [@code{--}]} @var{file} @dots{}
  255. @end example
  256.  
  257. The options that @code{gawk} accepts are:
  258.  
  259. @table @code
  260. @item -F@var{fs}
  261. Use @var{fs} for the input field separator (the value of the @code{FS}
  262. predefined variable).
  263.  
  264. @item -f @var{program-file}
  265. Read the @code{awk} program source from the file @var{program-file}, instead
  266. of from the first command line argument.
  267.  
  268. @item -v @var{var}=@var{val}
  269. Assign the variable @var{var} the value @var{val} before program execution
  270. begins.
  271.  
  272. @item -a
  273. Specifies use of traditional @code{awk} syntax for regular expressions.
  274. This means that @samp{\} can be used to quote regular expression
  275. operators inside of square brackets, just as it can be outside of them.
  276.  
  277. @item -e
  278. Specifies use of @code{egrep} syntax for regular expressions.  This
  279. means that @samp{\} does not serve as a quoting character inside of
  280. square brackets.
  281.  
  282. @item -c
  283. Specifies compatibility mode, in which @code{gawk} extensions are turned
  284. off.
  285.  
  286. @item -V
  287. Print version information for this particular copy of @code{gawk} on the error
  288. output.  This option may disappear in a future version of @code{gawk}.
  289.  
  290. @item -C
  291. Print the short version of the General Public License on the error
  292. output.  This option may disappear in a future version of @code{gawk}.
  293.  
  294. @item --
  295. Signal the end of options.  This is useful to allow further arguments to the
  296. @code{awk} program itself to start with a @samp{-}.  This is mainly for
  297. consistency with the argument parsing conventions of POSIX.
  298. @end table
  299.  
  300. Any other options are flagged as invalid, but are otherwise ignored.
  301. @xref{Command Line}, for more details.
  302.  
  303. @node Language Summary, Variables/Fields, Command Line Summary, Gawk Summary
  304. @appendixsec Language Summary
  305.  
  306. An @code{awk} program consists of a sequence of pattern-action statements
  307. and optional function definitions.
  308.  
  309. @example
  310. @var{pattern}    @{ @var{action statements} @}
  311.  
  312. function @var{name}(@var{parameter list})     @{ @var{action statements} @}
  313. @end example
  314.  
  315. @code{gawk} first reads the program source from the
  316. @var{program-file}(s) if specified, or from the first non-option
  317. argument on the command line.  The @samp{-f} option may be used multiple
  318. times on the command line.  @code{gawk} reads the program text from all
  319. the @var{program-file} files, effectively concatenating them in the
  320. order they are specified.  This is useful for building libraries of
  321. @code{awk} functions, without having to include them in each new
  322. @code{awk} program that uses them.  To use a library function in a file
  323. from a program typed in on the command line, specify @samp{-f /dev/tty};
  324. then type your program, and end it with a @kbd{C-d}.  @xref{Command
  325. Line}.
  326.  
  327. The environment variable @code{AWKPATH} specifies a search path to use
  328. when finding source files named with the @samp{-f} option.  If the
  329. variable @code{AWKPATH} is not set, @code{gawk} uses the default path,
  330. @samp{.:/usr/lib/awk:/usr/local/lib/awk}.  If a file name given to the
  331. @samp{-f} option contains a @samp{/} character, no path search is
  332. performed.  @xref{AWKPATH Variable}, for a full description of the
  333. @code{AWKPATH} environment variable.@refill
  334.  
  335. @code{gawk} compiles the program into an internal form, and then proceeds to
  336. read each file named in the @code{ARGV} array.  If there are no files named
  337. on the command line, @code{gawk} reads the standard input.
  338.  
  339. If a ``file'' named on the command line has the form
  340. @samp{@var{var}=@var{val}}, it is treated as a variable assignment: the
  341. variable @var{var} is assigned the value @var{val}.
  342.  
  343. For each line in the input, @code{gawk} tests to see if it matches any
  344. @var{pattern} in the @code{awk} program.  For each pattern that the line
  345. matches, the associated @var{action} is executed.
  346.  
  347. @node Variables/Fields, Rules Summary, Language Summary, Gawk Summary
  348. @appendixsec Variables and Fields
  349.  
  350. @code{awk} variables are dynamic; they come into existence when they are
  351. first used.  Their values are either floating-point numbers or strings.
  352. @code{awk} also has one-dimension arrays; multiple-dimensional arrays
  353. may be simulated.  There are several predefined variables that
  354. @code{awk} sets as a program runs; these are summarized below.
  355.  
  356. @menu
  357. * Fields Summary::      Input field splitting.
  358. * Built-in Summary::    @code{awk}'s built-in variables.
  359. * Arrays Summary::      Using arrays.
  360. * Data Type Summary::   Values in @code{awk} are numbers or strings.
  361. @end menu
  362.  
  363. @node Fields Summary, Built-in Summary, Variables/Fields, Variables/Fields
  364. @appendixsubsec Fields
  365.  
  366. As each input line is read, @code{gawk} splits the line into
  367. @var{fields}, using the value of the @code{FS} variable as the field
  368. separator.  If @code{FS} is a single character, fields are separated by
  369. that character.  Otherwise, @code{FS} is expected to be a full regular
  370. expression.  In the special case that @code{FS} is a single blank,
  371. fields are separated by runs of blanks and/or tabs.  Note that the value
  372. of @code{IGNORECASE} (@pxref{Case-sensitivity}) also affects how fields
  373. are split when @code{FS} is a regular expression.
  374.  
  375. Each field in the input line may be referenced by its position, @code{$1},
  376. @code{$2}, and so on.  @code{$0} is the whole line.  The value of a field may
  377. be assigned to as well.  Field numbers need not be constants:
  378.  
  379. @example
  380. n = 5
  381. print $n
  382. @end example
  383.  
  384. @noindent
  385. prints the fifth field in the input line.  The variable @code{NF} is set to
  386. the total number of fields in the input line.
  387.  
  388. References to nonexistent fields (i.e., fields after @code{$NF}) return
  389. the null-string.  However, assigning to a nonexistent field (e.g.,
  390. @code{$(NF+2) = 5}) increases the value of @code{NF}, creates any
  391. intervening fields with the null string as their value, and causes the
  392. value of @code{$0} to be recomputed, with the fields being separated by
  393. the value of @code{OFS}.@refill
  394.  
  395. @xref{Reading Files}, for a full description of the way @code{awk} defines
  396. and uses fields.
  397.  
  398. @node Built-in Summary, Arrays Summary, Fields Summary, Variables/Fields
  399. @appendixsubsec Built-in Variables
  400.  
  401. @code{awk}'s built-in variables are:
  402.  
  403. @table @code
  404. @item ARGC
  405. The number of command line arguments (not including options or the
  406. @code{awk} program itself).
  407.  
  408. @item ARGV
  409. The array of command line arguments.  The array is indexed from 0 to
  410. @code{ARGC} - 1.  Dynamically changing the contents of @code{ARGV} can control
  411. the files used for data.@refill
  412.  
  413. @item ENVIRON
  414. An array containing the values of the environment variables.  The array
  415. is indexed by variable name, each element being the value of that
  416. variable.  Thus, the environment variable @code{HOME} would be in
  417. @code{ENVIRON["HOME"]}.  Its value might be @file{/u/close}.
  418.  
  419. Changing this array does not affect the environment seen by programs
  420. which @code{gawk} spawns via redirection or the @code{system} function.
  421. (This may change in a future version of @code{gawk}.)
  422.  
  423. Some operating systems do not have environment variables.
  424. The array @code{ENVIRON} is empty when running on these systems.
  425.  
  426. @item FILENAME
  427. The name of the current input file.  If no files are specified on the command
  428. line, the value of @code{FILENAME} is @samp{-}.
  429.  
  430. @item FNR
  431. The input record number in the current input file.
  432.  
  433. @item FS
  434. The input field separator, a blank by default.
  435.  
  436. @item IGNORECASE
  437. The case-sensitivity flag for regular expression operations.  If
  438. @code{IGNORECASE} has a nonzero value, then pattern matching in rules,
  439. field splitting with @code{FS}, regular expression matching with
  440. @samp{~} and @samp{!~}, and the @code{gsub}, @code{index}, @code{match},
  441. @code{split} and @code{sub} predefined functions all ignore case
  442. when doing regular expression operations.@refill
  443.  
  444. @item NF
  445. The number of fields in the current input record.
  446.  
  447. @item NR
  448. The total number of input records seen so far.
  449.  
  450. @item OFMT
  451. The output format for numbers, @code{"%.6g"} by default.
  452.  
  453. @item OFS
  454. The output field separator, a blank by default.
  455.  
  456. @item ORS
  457. The output record separator, by default a newline.
  458.  
  459. @item RS
  460. The input record separator, by default a newline.  @code{RS} is exceptional
  461. in that only the first character of its string value is used for separating
  462. records.  If @code{RS} is set to the null string, then records are separated by
  463. blank lines.  When @code{RS} is set to the null string, then the newline
  464. character always acts as a field separator, in addition to whatever value
  465. @code{FS} may have.@refill
  466.  
  467. @item RSTART
  468. The index of the first character matched by @code{match}; 0 if no match.
  469.  
  470. @item RLENGTH
  471. The length of the string matched by @code{match}; @minus{}1 if no match.
  472.  
  473. @item SUBSEP
  474. The string used to separate multiple subscripts in array elements, by
  475. default @code{"\034"}.
  476. @end table
  477.  
  478. @xref{Built-in Variables}.
  479.  
  480. @node Arrays Summary, Data Type Summary, Built-in Summary, Variables/Fields
  481. @appendixsubsec Arrays
  482.  
  483. Arrays are subscripted with an expression between square brackets
  484. (@samp{[} and @samp{]}).  The expression may be either a number or
  485. a string.  Since arrays are associative, string indices are meaningful
  486. and are not converted to numbers.
  487.  
  488. If you use multiple expressions separated by commas inside the square
  489. brackets, then the array subscript is a string consisting of the
  490. concatenation of the individual subscript values, converted to strings,
  491. separated by the subscript separator (the value of @code{SUBSEP}).
  492.  
  493. The special operator @code{in} may be used in an @code{if} or
  494. @code{while} statement to see if an array has an index consisting of a
  495. particular value.
  496.  
  497. @group
  498. @example
  499. if (val in array)
  500.         print array[val]
  501. @end example
  502. @end group
  503.  
  504. If the array has multiple subscripts, use @code{(i, j, @dots{}) in array}
  505. to test for existence of an element.
  506.  
  507. The @code{in} construct may also be used in a @code{for} loop to iterate
  508. over all the elements of an array.  @xref{Scanning an Array}.
  509.  
  510. An element may be deleted from an array using the @code{delete} statement.
  511.  
  512. @xref{Arrays}, for more detailed information.
  513.  
  514. @node Data Type Summary, , Arrays Summary, Variables/Fields
  515. @appendixsubsec Data Types
  516.  
  517. The value of an @code{awk} expression is always either a number
  518. or a string.
  519.  
  520. Certain contexts (such as arithmetic operators) require numeric
  521. values.  They convert strings to numbers by interpreting the text
  522. of the string as a numeral.  If the string does not look like a
  523. numeral, it converts to 0.
  524.  
  525. Certain contexts (such as concatenation) require string values.
  526. They convert numbers to strings by effectively printing them.
  527.  
  528. To force conversion of a string value to a number, simply add 0
  529. to it.  If the value you start with is already a number, this
  530. does not change it.
  531.  
  532. To force conversion of a numeric value to a string, concatenate it with
  533. the null string.
  534.  
  535. The @code{awk} language defines comparisons as being done numerically if
  536. possible, otherwise one or both operands are converted to strings and
  537. a string comparison is performed.
  538.  
  539. Uninitialized variables have the string value @code{""} (the null, or
  540. empty, string).  In contexts where a number is required, this is
  541. equivalent to 0.
  542.  
  543. @xref{Variables}, for more information on variable naming and initialization;
  544. @pxref{Conversion}, for more information on how variable values are
  545. interpreted.@refill
  546.  
  547. @node Rules Summary, Functions Summary, Variables/Fields, Gawk Summary
  548. @appendixsec Patterns and Actions
  549.  
  550. @menu
  551. * Pattern Summary::     Quick overview of patterns.
  552. * Regexp Summary::      Quick overview of regular expressions.
  553. * Actions Summary::     Quick overview of actions.
  554. @end menu
  555.  
  556. An @code{awk} program is mostly composed of rules, each consisting of a
  557. pattern followed by an action.  The action is enclosed in @samp{@{} and
  558. @samp{@}}.  Either the pattern may be missing, or the action may be
  559. missing, but, of course, not both.  If the pattern is missing, the
  560. action is executed for every single line of input.  A missing action is
  561. equivalent to this action,
  562.  
  563. @example
  564. @{ print @}
  565. @end example
  566.  
  567. @noindent
  568. which prints the entire line.
  569.  
  570. Comments begin with the @samp{#} character, and continue until the end of the
  571. line.  Blank lines may be used to separate statements.  Normally, a statement
  572. ends with a newline, however, this is not the case for lines ending in a
  573. @samp{,}, @samp{@{}, @samp{?}, @samp{:}, @samp{&&}, or @samp{||}.  Lines
  574. ending in @code{do} or @code{else} also have their statements automatically
  575. continued on the following line.  In other cases, a line can be continued by
  576. ending it with a @samp{\}, in which case the newline is ignored.@refill
  577.  
  578. Multiple statements may be put on one line by separating them with a @samp{;}.
  579. This applies to both the statements within the action part of a rule (the
  580. usual case), and to the rule statements themselves.
  581.  
  582. @xref{Comments}, for information on @code{awk}'s commenting convention;
  583. @pxref{Statements/Lines}, for a description of the line continuation
  584. mechanism in @code{awk}.
  585.  
  586. @node Pattern Summary, Regexp Summary, Rules Summary, Rules Summary
  587. @appendixsubsec Patterns
  588.  
  589. @code{awk} patterns may be one of the following:
  590.  
  591. @example
  592. /@var{regular expression}/
  593. @var{relational expression}
  594. @var{pattern} && @var{pattern}
  595. @var{pattern} || @var{pattern}
  596. @var{pattern} ? @var{pattern} : @var{pattern}
  597. (@var{pattern})
  598. ! @var{pattern}
  599. @var{pattern1}, @var{pattern2}
  600. BEGIN
  601. END
  602. @end example
  603.  
  604. @code{BEGIN} and @code{END} are two special kinds of patterns that are not
  605. tested against the input.  The action parts of all @code{BEGIN} rules are
  606. merged as if all the statements had been written in a single @code{BEGIN}
  607. rule.  They are executed before any of the input is read.  Similarly, all the
  608. @code{END} rules are merged, and executed when all the input is exhausted (or
  609. when an @code{exit} statement is executed).  @code{BEGIN} and @code{END}
  610. patterns cannot be combined with other patterns in pattern expressions.
  611. @code{BEGIN} and @code{END} rules cannot have missing action parts.@refill
  612.  
  613. For @samp{/@var{regular-expression}/} patterns, the associated statement is
  614. executed for each input line that matches the regular expression.  Regular
  615. expressions are the same as those in @code{egrep}, and are summarized below.
  616.  
  617. A @var{relational expression} may use any of the operators defined below in
  618. the section on actions.  These generally test whether certain fields match
  619. certain regular expressions.
  620.  
  621. The @samp{&&}, @samp{||}, and @samp{!} operators are logical ``and'',
  622. logical ``or'', and logical ``not'', respectively, as in C.  They do
  623. short-circuit evaluation, also as in C, and are used for combining more
  624. primitive pattern expressions.  As in most languages, parentheses may be
  625. used to change the order of evaluation.
  626.  
  627. The @samp{?:} operator is like the same operator in C.  If the first
  628. pattern matches, then the second pattern is matched against the input
  629. record; otherwise, the third is matched.  Only one of the second and
  630. third patterns is matched.
  631.  
  632. The @samp{@var{pattern1}, @var{pattern2}} form of a pattern is called a
  633. range pattern.  It matches all input lines starting with a line that
  634. matches @var{pattern1}, and continuing until a line that matches
  635. @var{pattern2}, inclusive.  A range pattern cannot be used as an operand
  636. to any of the pattern operators.
  637.  
  638. @xref{Patterns}, for a full description of the pattern part of @code{awk}
  639. rules.
  640.  
  641. @node Regexp Summary, Actions Summary, Pattern Summary, Rules Summary
  642. @appendixsubsec Regular Expressions
  643.  
  644. Regular expressions are the extended kind found in @code{egrep}.
  645. They are composed of characters as follows:
  646.  
  647. @table @code
  648. @item @var{c}
  649. matches the character @var{c} (assuming @var{c} is a character with no
  650. special meaning in regexps).
  651.  
  652. @item \@var{c}
  653. matches the literal character @var{c}.
  654.  
  655. @item .
  656. matches any character except newline.
  657.  
  658. @item ^
  659. matches the beginning of a line or a string.
  660.  
  661. @item $
  662. matches the end of a line or a string.
  663.  
  664. @item [@var{abc}@dots{}]
  665. matches any of the characters @var{abc}@dots{} (character class).
  666.  
  667. @item [^@var{abc}@dots{}]
  668. matches any character except @var{abc}@dots{} and newline (negated
  669. character class).
  670.  
  671. @item @var{r1}|@var{r2}
  672. matches either @var{r1} or @var{r2} (alternation).
  673.  
  674. @item @var{r1r2}
  675. matches @var{r1}, and then @var{r2} (concatenation).
  676.  
  677. @item @var{r}+
  678. matches one or more @var{r}'s.
  679.  
  680. @item @var{r}*
  681. matches zero or more @var{r}'s. 
  682.  
  683. @item @var{r}?
  684. matches zero or one @var{r}'s. 
  685.  
  686. @item (@var{r})
  687. matches @var{r} (grouping).
  688. @end table
  689.  
  690. @xref{Regexp}, for a more detailed explanation of regular expressions.
  691.  
  692. The escape sequences allowed in string constants are also valid in
  693. regular expressions (@pxref{Constants}).
  694.  
  695. @node Actions Summary, , Regexp Summary, Rules Summary
  696. @appendixsubsec Actions
  697.  
  698. Action statements are enclosed in braces, @samp{@{} and @samp{@}}.
  699. Action statements consist of the usual assignment, conditional, and looping
  700. statements found in most languages.  The operators, control statements,
  701. and input/output statements available are patterned after those in C.
  702.  
  703. @menu
  704. * Operator Summary::            @code{awk} operators.
  705. * Control Flow Summary::        The control statements.
  706. * I/O Summary::                 The I/O statements.
  707. * Printf Summary::              A summary of @code{printf}.
  708. * Special File Summary::        Special file names interpreted internally.
  709. * Numeric Functions Summary::   Built-in numeric functions.
  710. * String Functions Summary::    Built-in string functions.
  711. * String Constants Summary::    Escape sequences in strings.
  712. @end menu
  713.  
  714. @node Operator Summary, Control Flow Summary, Actions Summary, Actions Summary
  715. @appendixsubsubsec Operators
  716.  
  717. The operators in @code{awk}, in order of increasing precedence, are
  718.  
  719. @table @code
  720. @item = += -= *= /= %= ^=
  721. Assignment.  Both absolute assignment (@code{@var{var}=@var{value}})
  722. and operator assignment (the other forms) are supported.
  723.  
  724. @item ?:
  725. A conditional expression, as in C.  This has the form @code{@var{expr1} ?
  726. @var{expr2} : @var{expr3}}.  If @var{expr1} is true, the value of the
  727. expression is @var{expr2}; otherwise it is @var{expr3}.  Only one of
  728. @var{expr2} and @var{expr3} is evaluated.@refill
  729.  
  730. @item ||
  731. Logical ``or''.
  732.  
  733. @item &&
  734. Logical ``and''.
  735.  
  736. @item ~ !~
  737. Regular expression match, negated match.
  738.  
  739. @item < <= > >= != ==
  740. The usual relational operators.
  741.  
  742. @item @var{blank}
  743. String concatenation.
  744.  
  745. @item + -
  746. Addition and subtraction.
  747.  
  748. @item * / %
  749. Multiplication, division, and modulus.
  750.  
  751. @item + - !
  752. Unary plus, unary minus, and logical negation.
  753.  
  754. @item ^
  755. Exponentiation (@samp{**} may also be used, and @samp{**=} for the assignment
  756. operator).
  757.  
  758. @item ++ --
  759. Increment and decrement, both prefix and postfix.
  760.  
  761. @item $
  762. Field reference.
  763. @end table
  764.  
  765. @xref{Expressions}, for a full description of all the operators listed
  766. above.  @xref{Fields}, for a description of the field reference operator.
  767.  
  768. @node Control Flow Summary, I/O Summary, Operator Summary, Actions Summary
  769. @appendixsubsubsec Control Statements
  770.  
  771. The control statements are as follows:
  772.  
  773. @example
  774. if (@var{condition}) @var{statement} @r{[} else @var{statement} @r{]}
  775. while (@var{condition}) @var{statement}
  776. do @var{statement} while (@var{condition})
  777. for (@var{expr1}; @var{expr2}; @var{expr3}) @var{statement}
  778. for (@var{var} in @var{array}) @var{statement}
  779. break
  780. continue
  781. delete @var{array}[@var{index}]
  782. exit @r{[} @var{expression} @r{]}
  783. @{ @var{statements} @}
  784. @end example
  785.  
  786. @xref{Statements}, for a full description of all the control statements
  787. listed above.
  788.  
  789. @node I/O Summary, Printf Summary, Control Flow Summary, Actions Summary
  790. @appendixsubsubsec I/O Statements
  791.  
  792. The input/output statements are as follows:
  793.  
  794. @table @code
  795. @item getline
  796. Set @code{$0} from next input record; set @code{NF}, @code{NR}, @code{FNR}.
  797.  
  798. @item getline <@var{file}
  799. Set @code{$0} from next record of @var{file}; set @code{NF}.
  800.  
  801. @item getline @var{var}
  802. Set @var{var} from next input record; set @code{NF}, @code{FNR}.
  803.  
  804. @item getline @var{var} <@var{file}
  805. Set @var{var} from next record of @var{file}.
  806.  
  807. @item next
  808. Stop processing the current input record.  The next input record is read and
  809. processing starts over with the first pattern in the @code{awk} program.
  810. If the end of the input data is reached, the @code{END} rule(s), if any,
  811. are executed.
  812.  
  813. @item print
  814. Prints the current record.
  815.  
  816. @item print @var{expr-list}
  817. Prints expressions.
  818.  
  819. @item print @var{expr-list} > @var{file}
  820. Prints expressions on @var{file}.
  821.  
  822. @item printf @var{fmt, expr-list}
  823. Format and print.
  824.  
  825. @item printf @var{fmt, expr-list} > file
  826. Format and print on @var{file}.
  827. @end table
  828.  
  829. Other input/output redirections are also allowed.  For @code{print} and
  830. @code{printf}, @samp{>> @var{file}} appends output to the @var{file},
  831. while @samp{| @var{command}} writes on a pipe.  In a similar fashion,
  832. @samp{@var{command} | getline} pipes input into @code{getline}.
  833. @code{getline} returns 0 on end of file, and @minus{}1 on an error.@refill
  834.  
  835. @xref{Getline}, for a full description of the @code{getline} statement.
  836. @xref{Printing}, for a full description of @code{print} and
  837. @code{printf}.  Finally, @pxref{Next Statement}, for a description of
  838. how the @code{next} statement works.@refill
  839.  
  840. @node Printf Summary, Special File Summary, I/O Summary, Actions Summary
  841. @appendixsubsubsec @code{printf} Summary
  842.  
  843. The @code{awk} @code{printf} statement and @code{sprintf} function
  844. accept the following conversion specification formats:
  845.  
  846. @table @code
  847. @item %c
  848. An ASCII character.  If the argument used for @samp{%c} is numeric, it is
  849. treated as a character and printed.  Otherwise, the argument is assumed to
  850. be a string, and the only first character of that string is printed.
  851.  
  852. @item %d
  853. A decimal number (the integer part).
  854.  
  855. @item %i
  856. Also a decimal integer.
  857.  
  858. @item %e
  859. A floating point number of the form
  860. @samp{@r{[}-@r{]}d.ddddddE@r{[}+-@r{]}dd}.@refill
  861.  
  862. @item %f
  863. A floating point number of the form
  864. @r{[}@code{-}@r{]}@code{ddd.dddddd}.
  865.  
  866. @item %g
  867. Use @samp{%e} or @samp{%f} conversion, whichever is shorter, with
  868. nonsignificant zeros suppressed.
  869.  
  870. @item %o
  871. An unsigned octal number (again, an integer).
  872.  
  873. @item %s
  874. A character string.
  875.  
  876. @item %x
  877. An unsigned hexadecimal number (an integer).
  878.  
  879. @item %X
  880. Like @samp{%x}, except use @samp{A} through @samp{F} instead of @samp{a}
  881. through @samp{f} for decimal 10 through 15.@refill
  882.  
  883. @item %%
  884. A single @samp{%} character; no argument is converted.
  885. @end table
  886.  
  887. There are optional, additional parameters that may lie between the @samp{%}
  888. and the control letter:
  889.  
  890. @table @code
  891. @item -
  892. The expression should be left-justified within its field.
  893.  
  894. @item @var{width}
  895. The field should be padded to this width.  If @var{width} has a leading zero,
  896. then the field is padded with zeros.  Otherwise it is padded with blanks.
  897.  
  898. @item .@var{prec}
  899. A number indicating the maximum width of strings or digits to the right
  900. of the decimal point.
  901. @end table
  902.  
  903. @xref{Printf}, for examples and for a more detailed description.
  904.  
  905. @node Special File Summary, Numeric Functions Summary, Printf Summary, Actions Summary
  906. @appendixsubsubsec Special File Names
  907.  
  908. When doing I/O redirection from either @code{print} or @code{printf} into a
  909. file, or via @code{getline} from a file, @code{gawk} recognizes certain special
  910. file names internally.  These file names allow access to open file descriptors
  911. inherited from @code{gawk}'s parent process (usually the shell).  The
  912. file names are:
  913.  
  914. @table @file
  915. @item /dev/stdin
  916. The standard input.
  917.  
  918. @item /dev/stdout
  919. The standard output.
  920.  
  921. @item /dev/stderr
  922. The standard error output.
  923.  
  924. @item /dev/fd/@var{n}
  925. The file denoted by the open file descriptor @var{n}.
  926. @end table
  927.  
  928. @noindent
  929. These file names may also be used on the command line to name data files.
  930.  
  931. @xref{Special Files}, for a longer description that provides the motivation
  932. for this feature.
  933.  
  934. @node Numeric Functions Summary, String Functions Summary, Special File Summary, Actions Summary
  935. @appendixsubsubsec Numeric Functions
  936.  
  937. @code{awk} has the following predefined arithmetic functions:
  938.  
  939. @table @code
  940. @item atan2(@var{y}, @var{x})
  941. returns the arctangent of @var{y/x} in radians.
  942.  
  943. @item cos(@var{expr})
  944. returns the cosine in radians.
  945.  
  946. @item exp(@var{expr})
  947. the exponential function.
  948.  
  949. @item int(@var{expr})
  950. truncates to integer.
  951.  
  952. @item log(@var{expr})
  953. the natural logarithm function.
  954.  
  955. @item rand()
  956. returns a random number between 0 and 1.
  957.  
  958. @item sin(@var{expr})
  959. returns the sine in radians.
  960.  
  961. @item sqrt(@var{expr})
  962. the square root function.
  963.  
  964. @item srand(@var{expr})
  965. use @var{expr} as a new seed for the random number generator.  If no @var{expr}
  966. is provided, the time of day is used.  The return value is the previous
  967. seed for the random number generator.
  968. @end table
  969.  
  970. @node String Functions Summary, String Constants Summary, Numeric Functions Summary, Actions Summary
  971. @appendixsubsubsec String Functions
  972.  
  973. @code{awk} has the following predefined string functions:
  974.  
  975. @table @code
  976. @item gsub(@var{r}, @var{s}, @var{t})
  977. for each substring matching the regular expression @var{r} in the string
  978. @var{t}, substitute the string @var{s}, and return the number of substitutions.
  979. If @var{t} is not supplied, use @code{$0}.
  980.  
  981. @item index(@var{s}, @var{t})
  982. returns the index of the string @var{t} in the string @var{s}, or 0 if
  983. @var{t} is not present.
  984.  
  985. @item length(@var{s})
  986. returns the length of the string @var{s}.
  987.  
  988. @item match(@var{s}, @var{r})
  989. returns the position in @var{s} where the regular expression @var{r}
  990. occurs, or 0 if @var{r} is not present, and sets the values of @code{RSTART}
  991. and @code{RLENGTH}.
  992.  
  993. @item split(@var{s}, @var{a}, @var{r})
  994. splits the string @var{s} into the array @var{a} on the regular expression
  995. @var{r}, and returns the number of fields.  If @var{r} is omitted, @code{FS}
  996. is used instead.
  997.  
  998. @item sprintf(@var{fmt}, @var{expr-list})
  999. prints @var{expr-list} according to @var{fmt}, and returns the resulting string.
  1000.  
  1001. @item sub(@var{r}, @var{s}, @var{t})
  1002. this is just like @code{gsub}, but only the first matching substring is
  1003. replaced.
  1004.  
  1005. @item substr(@var{s}, @var{i}, @var{n})
  1006. returns the @var{n}-character substring of @var{s} starting at @var{i}.
  1007. If @var{n} is omitted, the rest of @var{s} is used.
  1008.  
  1009. @item tolower(@var{str})
  1010. returns a copy of the string @var{str}, with all the upper-case characters in
  1011. @var{str} translated to their corresponding lower-case counterparts.
  1012. Nonalphabetic characters are left unchanged.
  1013.  
  1014. @item toupper(@var{str})
  1015. returns a copy of the string @var{str}, with all the lower-case characters in
  1016. @var{str} translated to their corresponding upper-case counterparts.
  1017. Nonalphabetic characters are left unchanged.
  1018.  
  1019. @item system(@var{cmd-line})
  1020. Execute the command @var{cmd-line}, and return the exit status.
  1021. @end table
  1022.  
  1023. @xref{Built-in}, for a description of all of @code{awk}'s built-in functions.
  1024.  
  1025. @node String Constants Summary, , String Functions Summary, Actions Summary
  1026. @appendixsubsubsec String Constants
  1027.  
  1028. String constants in @code{awk} are sequences of characters enclosed
  1029. between double quotes (@code{"}).  Within strings, certain @dfn{escape sequences}
  1030. are recognized, as in C.  These are:
  1031.  
  1032. @table @code
  1033. @item \\
  1034. A literal backslash.
  1035.  
  1036. @item \a
  1037. The ``alert'' character; usually the ASCII BEL character.
  1038.  
  1039. @item \b
  1040. Backspace.
  1041.  
  1042. @item \f
  1043. Formfeed.
  1044.  
  1045. @item \n
  1046. Newline.
  1047.  
  1048. @item \r
  1049. Carriage return.
  1050.  
  1051. @item \t
  1052. Horizontal tab.
  1053.  
  1054. @item \v
  1055. Vertical tab.
  1056.  
  1057. @item \x@var{hex digits}
  1058. The character represented by the string of hexadecimal digits following
  1059. the @samp{\x}.  As in ANSI C, all following hexadecimal digits are
  1060. considered part of the escape sequence.  (This feature should tell us
  1061. something about language design by committee.)  E.g., @code{"\x1B"} is a
  1062. string containing the ASCII ESC (escape) character.
  1063.  
  1064. @item \@var{ddd}
  1065. The character represented by the 1-, 2-, or 3-digit sequence of octal
  1066. digits.  Thus, @code{"\033"} is also a string containing the ASCII ESC
  1067. (escape) character.
  1068.  
  1069. @item \@var{c}
  1070. The literal character @var{c}.
  1071. @end table
  1072.  
  1073. The escape sequences may also be used inside constant regular expressions
  1074. (e.g., the regexp @code{@w{/[@ \t\f\n\r\v]/}} matches whitespace
  1075. characters).@refill
  1076.  
  1077. @xref{Constants}.
  1078.  
  1079. @node Functions Summary, , Rules Summary, Gawk Summary
  1080. @appendixsec Functions
  1081.  
  1082. Functions in @code{awk} are defined as follows:
  1083.  
  1084. @example
  1085. function @var{name}(@var{parameter list}) @{ @var{statements} @}
  1086. @end example
  1087.  
  1088. Actual parameters supplied in the function call are used to instantiate
  1089. the formal parameters declared in the function.  Arrays are passed by
  1090. reference, other variables are passed by value.
  1091.  
  1092. If there are fewer arguments passed than there are names in @var{parameter-list},
  1093. the extra names are given the null string as value.  Extra names have the
  1094. effect of local variables.
  1095.  
  1096. The open-parenthesis in a function call must immediately follow the
  1097. function name, without any intervening white space.  This is to avoid a
  1098. syntactic ambiguity with the concatenation operator.
  1099.  
  1100. The word @code{func} may be used in place of @code{function}.
  1101.  
  1102. @xref{User-defined}, for a more complete description.
  1103.  
  1104. @node Sample Program, Notes, Gawk Summary, Top
  1105. @appendix Sample Program
  1106.  
  1107. The following example is a complete @code{awk} program, which prints
  1108. the number of occurrences of each word in its input.  It illustrates the
  1109. associative nature of @code{awk} arrays by using strings as subscripts.  It
  1110. also demonstrates the @samp{for @var{x} in @var{array}} construction.
  1111. Finally, it shows how @code{awk} can be used in conjunction with other
  1112. utility programs to do a useful task of some complexity with a minimum of
  1113. effort.  Some explanations follow the program listing.@refill
  1114.  
  1115. @example
  1116. awk '
  1117. # Print list of word frequencies
  1118. @{
  1119.     for (i = 1; i <= NF; i++)
  1120.         freq[$i]++
  1121. @}
  1122.  
  1123. END @{
  1124.     for (word in freq)
  1125.         printf "%s\t%d\n", word, freq[word]
  1126. @}'
  1127. @end example
  1128.  
  1129. The first thing to notice about this program is that it has two rules.  The
  1130. first rule, because it has an empty pattern, is executed on every line of
  1131. the input.  It uses @code{awk}'s field-accessing mechanism (@pxref{Fields})
  1132. to pick out the individual words from the line, and the built-in variable
  1133. @code{NF} (@pxref{Built-in Variables}) to know how many fields are available.
  1134.  
  1135. For each input word, an element of the array @code{freq} is incremented to
  1136. reflect that the word has been seen an additional time.@refill
  1137.  
  1138. The second rule, because it has the pattern @code{END}, is not executed
  1139. until the input has been exhausted.  It prints out the contents of the
  1140. @code{freq} table that has been built up inside the first action.@refill
  1141.  
  1142. Note that this program has several problems that would prevent it from being
  1143. useful by itself on real text files:@refill
  1144.  
  1145. @itemize @bullet
  1146. @item
  1147. Words are detected using the @code{awk} convention that fields are
  1148. separated by whitespace and that other characters in the input (except
  1149. newlines) don't have any special meaning to @code{awk}.  This means that
  1150. punctuation characters count as part of words.@refill
  1151.  
  1152. @item
  1153. The @code{awk} language considers upper and lower case characters to be
  1154. distinct.  Therefore, @samp{foo} and @samp{Foo} are not treated by this
  1155. program as the same word.  This is undesirable since in normal text, words
  1156. are capitalized if they begin sentences, and a frequency analyzer should not
  1157. be sensitive to that.@refill
  1158.  
  1159. @item
  1160. The output does not come out in any useful order.  You're more likely to be
  1161. interested in which words occur most frequently, or having an alphabetized
  1162. table of how frequently each word occurs.@refill
  1163. @end itemize
  1164.  
  1165. The way to solve these problems is to use other system utilities to
  1166. process the input and output of the @code{awk} script.  Suppose the
  1167. script shown above is saved in the file @file{frequency.awk}.  Then the
  1168. shell command:@refill
  1169.  
  1170. @example
  1171. tr A-Z a-z < file1 | tr -cd 'a-z\012' \
  1172.   | awk -f frequency.awk \
  1173.   | sort +1 -nr
  1174. @end example
  1175.  
  1176. @noindent
  1177. produces a table of the words appearing in @file{file1} in order of
  1178. decreasing frequency.
  1179.  
  1180. The first @code{tr} command in this pipeline translates all the upper case
  1181. characters in @file{file1} to lower case.  The second @code{tr} command
  1182. deletes all the characters in the input except lower case characters and
  1183. newlines.  The second argument to the second @code{tr} is quoted to protect
  1184. the backslash in it from being interpreted by the shell.  The @code{awk}
  1185. program reads this suitably massaged data and produces a word frequency
  1186. table, which is not ordered.
  1187.  
  1188. The @code{awk} script's output is now sorted by the @code{sort} command and
  1189. printed on the terminal.  The options given to @code{sort} in this example
  1190. specify to sort by the second field of each input line (skipping one field),
  1191. that the sort keys should be treated as numeric quantities (otherwise
  1192. @samp{15} would come before @samp{5}), and that the sorting should be done
  1193. in descending (reverse) order.@refill
  1194.  
  1195. See the general operating system documentation for more information on how
  1196. to use the @code{tr} and @code{sort} commands.@refill
  1197.  
  1198. @ignore
  1199. @strong{ADR: I have some more substantial programs courtesy of Rick Adams
  1200. at UUNET.  I am planning on incorporating those either in addition to or
  1201. instead of this program.}
  1202.  
  1203. @strong{I would also like to incorporate the general @code{translate}
  1204. function that I have written.}
  1205. @end ignore
  1206.  
  1207. @node Notes, Glossary, Sample Program, Top
  1208. @appendix Implementation Notes
  1209.  
  1210. This appendix contains information mainly of interest to implementors and
  1211. maintainers of @code{gawk}.  Everything in it applies specifically to
  1212. @code{gawk}, and not to other implementations.
  1213.  
  1214. @menu
  1215. * Compatibility Mode::   How to disable certain @code{gawk} extensions.
  1216.  
  1217. * Future Extensions::    New features we may implement soon.
  1218.  
  1219. * Improvements::         Suggestions for improvements by volunteers.
  1220. @end menu
  1221.  
  1222. @node Compatibility Mode, Future Extensions, Notes, Notes
  1223. @appendixsec Downwards Compatibility and Debugging
  1224.  
  1225. @xref{S5R4/GNU}, for a summary of the GNU extensions to the @code{awk}
  1226. language and program.  All of these features can be turned off either by
  1227. compiling @code{gawk} with @samp{-DSTRICT} (not recommended), or by
  1228. invoking @code{gawk} with the @samp{-c} option.@refill
  1229.  
  1230. If @code{gawk} is compiled for debugging with @samp{-DDEBUG}, then there
  1231. are two more options available on the command line.
  1232.  
  1233. @table @samp
  1234. @item -d
  1235. Print out debugging information during execution.
  1236.  
  1237. @item -D
  1238. Print out the parse stack information as the program is being parsed.
  1239. @end table
  1240.  
  1241. Both of these options are intended only for serious @code{gawk} developers,
  1242. and not for the casual user.  They probably have not even been compiled into
  1243. your version of @code{gawk}, since they slow down execution.
  1244.  
  1245. The code for recognizing special file names such as @file{/dev/stdin}
  1246. can be disabled at compile time with @samp{-DNO_DEV_FD}, or with
  1247. @samp{-DSTRICT}.@refill
  1248.  
  1249. @node Future Extensions, Improvements, Compatibility Mode, Notes
  1250. @appendixsec Probable Future Extensions
  1251.  
  1252. This section briefly lists extensions that indicate the directions we are
  1253. currently considering for @code{gawk}.
  1254.  
  1255. @table @asis
  1256. @item ANSI C compatible @code{printf}
  1257. The @code{printf} and @code{sprintf} functions may be enhanced to be
  1258. fully compatible with the specification for the @code{printf} family
  1259. of functions in ANSI C.@refill
  1260.  
  1261. @item @code{RS} as a regexp
  1262. The meaning of @code{RS} may be generalized along the lines of @code{FS}.
  1263.  
  1264. @item Control of subprocess environment
  1265. Changes made in @code{gawk} to the array @code{ENVIRON} may be
  1266. propagated to subprocesses run by @code{gawk}.
  1267.  
  1268. @item Data bases
  1269. It may be possible to map an NDBM/GDBM file into an @code{awk} array.
  1270.  
  1271. @item Single-character fields
  1272. The null string, @code{""}, as a field separator, will cause field
  1273. splitting and the split function to separate individual characters.
  1274. Thus, @code{split(a, "abcd", "")} would yield @code{a[1] == "a"},
  1275. @code{a[2] == "b"}, and so on.
  1276.  
  1277. @item Fixed-length fields and records
  1278. A mechanism may be provided to allow the specification of fixed length
  1279. fields and records.
  1280.  
  1281. @item Regexp syntax
  1282. The @code{egrep} syntax for regular expressions, now specified
  1283. with the @samp{-e} option, may become the default, since the
  1284. POSIX standard may specify this.
  1285.  
  1286. @c this is @emph{very} long term --- not worth including right now.
  1287. @ignore
  1288. @item The C Comma Operator
  1289. We may add the C comma operator, which takes the form
  1290. @code{@var{expr1},@var{expr2}}.  The first expression is evaluated, and the
  1291. result is thrown away.  The value of the full expression is the value of
  1292. @var{expr2}.@refill
  1293. @end ignore
  1294. @end table
  1295.  
  1296. @node Improvements,, Future Extensions, Notes
  1297. @appendixsec Suggestions for Improvements
  1298.  
  1299. Here are some projects that would-be @code{gawk} hackers might like to take
  1300. on.  They vary in size from a few days to a few weeks of programming,
  1301. depending on which one you choose and how fast a programmer you are.  Please
  1302. send any improvements you write to the maintainers at the GNU
  1303. project.@refill
  1304.  
  1305. @enumerate
  1306. @item
  1307. State machine regexp matcher: At present, @code{gawk} uses the
  1308. backtracking regular expression matcher from the GNU subroutine library.
  1309. If a regexp is really going to be used a lot of times, it is faster to
  1310. convert it once to a description of a finite state machine, then run a
  1311. routine simulating that machine every time you want to match the regexp.
  1312. You might be able to use the matching routines used by GNU @code{egrep}.
  1313.  
  1314. @item
  1315. Compilation of @code{awk} programs: @code{gawk} uses a Bison (YACC-like)
  1316. parser to convert the script given it into a syntax tree; the syntax
  1317. tree is then executed by a simple recursive evaluator.  Both of these
  1318. steps incur a lot of overhead, since parsing can be slow (especially if
  1319. you also do the previous project and convert regular expressions to
  1320. finite state machines at compile time) and the recursive evaluator
  1321. performs many procedure calls to do even the simplest things.@refill
  1322.  
  1323. It should be possible for @code{gawk} to convert the script's parse tree
  1324. into a C program which the user would then compile, using the normal
  1325. C compiler and a special @code{gawk} library to provide all the needed
  1326. functions (regexps, fields, associative arrays, type coercion, and so
  1327. on).@refill
  1328.  
  1329. An easier possibility might be for an intermediate phase of @code{awk} to
  1330. convert the parse tree into a linear byte code form like the one used
  1331. in GNU Emacs Lisp.  The recursive evaluator would then be replaced by
  1332. a straight line byte code interpreter that would be intermediate in speed
  1333. between running a compiled program and doing what @code{gawk} does
  1334. now.@refill
  1335.  
  1336. @item
  1337. An error message section has not been included in this version of the
  1338. manual.  Perhaps some nice beta testers will document some of the messages
  1339. for the future.
  1340. @end enumerate
  1341.  
  1342. @node Glossary, Index , Notes, Top
  1343. @appendix Glossary
  1344.  
  1345. @table @asis
  1346. @item Action
  1347. A series of @code{awk} statements attached to a rule.  If the rule's
  1348. pattern matches an input record, the @code{awk} language executes the
  1349. rule's action.  Actions are always enclosed in curly braces.
  1350. @xref{Actions}.@refill
  1351.  
  1352. @item Amazing @code{awk} Assembler
  1353. Henry Spencer at the University of Toronto wrote a retargetable assembler
  1354. completely as @code{awk} scripts.  It is thousands of lines long, including
  1355. machine descriptions for several 8-bit microcomputers.  It is distributed
  1356. with @code{gawk} and is a good example of a program that would have been
  1357. better written in another language.@refill
  1358.  
  1359. @item Assignment
  1360. An @code{awk} expression that changes the value of some @code{awk}
  1361. variable or data object.  An object that you can assign to is called an
  1362. @dfn{lvalue}.  @xref{Assignment Ops}.@refill
  1363.  
  1364. @item @code{awk} Language
  1365. The language in which @code{awk} programs are written.
  1366.  
  1367. @item @code{awk} Program
  1368. An @code{awk} program consists of a series of @dfn{patterns} and
  1369. @dfn{actions}, collectively known as @dfn{rules}.  For each input record
  1370. given to the program, the program's rules are all processed in turn.
  1371. @code{awk} programs may also contain function definitions.@refill
  1372.  
  1373. @item @code{awk} Script
  1374. Another name for an @code{awk} program.
  1375.  
  1376. @item Built-in Function
  1377. The @code{awk} language provides built-in functions that perform various
  1378. numerical and string computations.  Examples are @code{sqrt} (for the
  1379. square root of a number) and @code{substr} (for a substring of a
  1380. string).  @xref{Built-in}.@refill
  1381.  
  1382. @item Built-in Variable
  1383. The variables @code{ARGC}, @code{ARGV}, @code{ENVIRON},  @code{FILENAME},
  1384. @code{FNR}, @code{FS}, @code{NF}, @code{IGNORECASE}, @code{NR}, @code{OFMT},
  1385. @code{OFS}, @code{ORS}, @code{RLENGTH}, @code{RSTART}, @code{RS}, and
  1386. @code{SUBSEP}, have special meaning to @code{awk}.  Changing some of them
  1387. affects @code{awk}'s running environment.  @xref{Built-in Variables}.@refill
  1388.  
  1389. @item C
  1390. The system programming language that most GNU software is written in.  The
  1391. @code{awk} programming language has C-like syntax, and this manual
  1392. points out similarities between @code{awk} and C when appropriate.@refill
  1393.  
  1394. @item Compound Statement
  1395. A series of @code{awk} statements, enclosed in curly braces.  Compound
  1396. statements may be nested.  @xref{Statements}.@refill
  1397.  
  1398. @item Concatenation
  1399.