Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / awk / doc / awkcard.in
1 .\" AWK Reference Card --- Arnold Robbins, arnold@gnu.org
2 .\"
3 .\" Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 .\" 
5 .\" Permission is granted to make and distribute verbatim copies of
6 .\" this reference card provided the copyright notice and this permission
7 .\" notice are preserved on all copies.
8 .\" 
9 .\" Permission is granted to process this file through troff and print the
10 .\" results, provided the printed document carries copying permission
11 .\" notice identical to this one except for the removal of this paragraph
12 .\" (this paragraph not being relevant to the printed reference card).
13 .\" 
14 .\" Permission is granted to copy and distribute modified versions of this
15 .\" reference card under the conditions for verbatim copying, provided that
16 .\" the entire resulting derived work is distributed under the terms of a
17 .\" permission notice identical to this one.
18 .\" 
19 .\" Permission is granted to copy and distribute translations of this
20 .\" reference card into another language, under the above conditions for
21 .\" modified versions, except that this permission notice may be stated in
22 .\" a translation approved by the Foundation.
23 .\"
24 .\" Strings to save typing
25 .ds AK \*(FCawk\*(FR
26 .ds GK \*(FCgawk\*(FR
27 .ds NK Bell Labs \*(FCawk\*(FR
28 .ds MK \*(FCmawk\*(FR
29 .\"
30 .\"
31 .de TD\" tab defaults
32 .ta .2i .78i 1i 1.2i 1.4i 1.7i
33 ..
34 .de TE
35 .TD
36 ..
37
38 .sp
39 .ce
40 \*(CD\f(HB\s+8AWK REFERENCE\s0\*(FR
41 .sp
42 .\" --- Table Of Contents
43 .ta 2.4i 2.6iR
44 .lc .
45 .ES
46 .in +.2i
47 .nf
48 \*(FRAWK Program Execution\ 1     4
49 Action Statements\ 1      7
50 Arrays\ 1 9
51 Bug Reports\ 1    15
52 Command Line Arguments (standard)\ 1      2
53 Command Line Arguments (\*(GK)\ 1 3
54 Command Line Arguments (\*(MK)\ 1 4
55 Conversions And Comparisons\ 1    10
56 Copying Permissions\ 1    16
57 Definitions\ 1    2
58 Environment Variables\ 1  16
59 Escape Sequences\ 1       7
60 Expressions\ 1    9
61 Fields\ 1 6
62 FTP Information\ 1        16
63 Historical Features (\*(GK)\ 1    16
64 Input Control\ 1  11
65 Lines And Statements\ 1   5
66 .ig
67 Localization\ 1   10
68 ..
69 Numeric Functions\ 1      13
70 Output Control\ 1 11
71 Pattern Elements\ 1       7
72 POSIX Character Classes (\*(GK)\ 1        6
73 Printf Formats\ 1 12
74 Records\ 1        6
75 Regular Expressions\ 1    5
76 Special Filenames\ 1      13
77 String Functions\ 1       14
78 Time Functions (\*(GK)\ 1 15
79 User-defined Functions\ 1 15
80 Variables\ 1      8\*(CX
81 .in -.2i
82 .EB "\s+2\f(HBCONTENTS\*(FR\s0"
83 .sp
84 .TD
85 .fi
86 \*(CD\*(FRThis reference card was written by Arnold Robbins.
87 Brian Kernighan and Michael Brennan reviewed it; we thank them
88 for their help.
89 .sp
90 .SL
91 .sp
92 .so SRCDIR/ad.block
93 .\" a subtlety here; this line changes color. We rely on it
94 .\" also to provide a blank line.
95 \*(CD
96 .SL
97 .nf
98 \*(FR\(co Copyright 1996-2000, Free Software Foundation
99 59 Temple Place \(em Suite 330
100 Boston, MA  02111-1307 USA
101 .nf
102 .BT
103
104
105 .\"
106 .\"
107 .\" --- Definitions
108 .fi
109 .ES
110 \*(CDThis card describes POSIX AWK, as well as the three
111 freely available \*(AK implementations
112 (see \fHFTP Information\fP below).
113 \*(CLCommon extensions (in two or more versions) are printed in light blue.
114 \*(CBFeatures specific to just one version\(emusually GNU AWK (\*(GK)\(emare
115 printed in dark blue.
116 \*(CRExceptions and deprecated features are printed in red.
117 \*(CDFeatures mandated by POSIX are printed in black.
118 .sp .5
119 Several type faces are used to clarify the meaning:
120 .br
121 .nr IN \w'\(bu '
122 \(bu \*(FC\*(CN\fP is used for computer input.
123 .br
124 .fi
125 .in +\n(INu
126 .ti -\n(INu
127 \(bu\|\^\*(FI\*(IN\fP is used to indicate user input and for syntactic
128 placeholders, such as \*(FIvariable\fP or \*(FIaction\fP.
129 .in -\n(INu
130 .br
131 \(bu \*(RN is used for explanatory text.
132 .sp .5
133 \*(FInumber\fP \- a floating point number as in ANSI C, such as
134 \*(FC3\*(FR,
135 \*(FC2.3\*(FR,
136 \*(FC.4\*(FR,
137 \*(FC1.4e2\*(FR
138 or
139 \*(FC4.1E5\*(FR.
140 .sp .5
141 \*(FIescape sequences\fP \- a special sequence of characters beginning
142 with a backslash, used to describe otherwise unprintable characters.
143 (See \fHEscape Sequences\fP below.)
144 .sp .5
145 \*(FIstring\fP \- a group of characters enclosed in double quotes.
146 Strings may contain \*(FIescape sequences\*(FR.
147 .sp .5
148 \*(FIregexp\fP \- a regular expression, either a regexp constant
149 enclosed in forward slashes, or a dynamic regexp computed at run-time.
150 Regexp constants may contain \*(FIescape sequences\*(FR.
151 .sp .5
152 \*(FIname\fP \- a variable, array or function name.
153 .sp .5
154 \*(FIentry\fP(\*(FIN\fP) \- entry \*(FIentry\fP in section \*(FIN\fP of the
155 UNIX reference manual.
156 .sp .5
157 \*(FIpattern\fP \- an expression describing an input record to be matched.
158 .sp .5
159 \*(FIaction\fP \- statements to execute when an input record is matched.
160 .sp .5
161 \*(FIrule\fP \- a pattern-action pair, where the pattern or action may
162 be missing.\*(CX
163 .EB \s+2\f(HBDEFINITIONS\*(FR\s0
164
165 .\" --- Command Line Arguments
166 .ES
167 .fi
168 \*(CDCommand line arguments control setting the field separator,
169 setting variables before the \*(FCBEGIN\fP rule is run, and
170 the location of AWK program source code.
171 Implementation-specific command line arguments change
172 the behavior of the running interpreter.
173 .sp .5
174 .TS
175 expand;
176 l lw(2.2i).
177 \*(FC\-F \*(FIfs\*(FR   use \*(FIfs\fP for the input field separator.
178 \*(FC\-v\*(FI var\*(FC\^=\^\*(FIval\*(FR        T{
179 assign the value \*(FIval\*(FR, to the variable \*(FIvar\*(FR,
180 before execution of the program begins. Such
181 variable values are available to the \*(FCBEGIN\fP rule.
182 T}
183 \*(FC\-f \*(FIprog-file\*(FR    T{
184 read the AWK program source from the file
185 \*(FIprog-file\*(FR, instead of from the first command
186 line argument. Multiple \*(FC\-f\*(FR options may be used.
187 T}
188 \*(FC\-\^\-\*(FR        signal the end of options.
189 .TE
190 .sp .5
191 .fi
192 \*(CLThe following options are accepted by both \*(NK and \*(GK
193 \*(CR(ignored by \*(GK, not in \*(MK).\*(CL
194 .sp .5
195 .nf
196 .TS
197 expand, tab(%);
198 l lw(2.2i).
199 \*(FC\-mf \*(FIval\*(FR%set the maximum number of fields to \*(FIval\fP
200 \*(FC\-mr \*(FIval\*(FR%set the maximum record size to \*(FIval\fP\*(CX
201 .TE
202 .EB "\s+2\f(HBCOMMAND LINE ARGUMENTS (standard)\*(FR\s0"
203
204 .BT
205
206 .ES
207 .fi
208 \*(CDThe following options are specific to \*(GK. The \*(FC\-W\*(FR
209 forms are for full POSIX compliance.
210 .sp .5
211 .ig
212 .\" This option is left undocumented, on purpose.
213 \*(FC\-\^\-nostalgia\*(FR
214 \*(FC\-W nostalgia\*(FR%T{
215 provide a moment of nostalgia for
216 long time \*(AK users.
217 T}
218 ..
219 .TS
220 expand, tab(%);
221 ls
222 l lw(1.8i).
223 \*(FC\-\^\-field-separator \*(FIfs\*(FR
224 %just like \*(FC\-F\fP
225 \*(FC\-\^\-assign \*(FIvar\*(FC\^=\^\*(FIval\*(FR%just like \*(FC\-v\fP
226 \*(FC\-\^\-file \*(FIprog-file%\*(FRjust like \*(FC\-f\fP
227 \*(FC\-\^\-traditional\*(FR
228 \*(FC\-\^\-compat\*(FR
229 \*(FC\-W compat\*(FR
230 \*(FC\-W traditional\*(FR%T{
231 turn off \*(GK-specific extensions
232 (\*(FC\-\^\-traditional\*(FR preferred).
233 T}
234 \*(FC\-\^\-copyleft\*(FR
235 \*(FC\-\^\-copyright\*(FR
236 \*(FC\-W copyleft\*(FR
237 \*(FC\-W copyright\*(FR%T{
238 print the short version of the GNU
239 copyright information on \*(FCstdout\*(FR.
240 T}
241 \*(FC\-\^\-help\*(FR
242 \*(FC\-\^\-usage\*(FR
243 \*(FC\-W help\*(FR
244 \*(FC\-W usage\*(FR%T{
245 print a short summary of the available
246 options on \*(FCstdout\*(FR, then exit zero.
247 T}
248 \*(FC\-\^\-lint\*(FR
249 \*(FC\-W lint\*(FR%T{
250 warn about constructs that are dubious
251 or non-portable to other \*(AKs.
252 T}
253 \*(FC\-\^\-lint\-old\*(FR
254 \*(FC\-W lint\-old\*(FR%T{
255 warn about constructs that are not
256 portable to the original version of
257 Unix \*(AK.
258 T}
259 \*(FC\-\^\-posix\*(FR
260 \*(FC\-W posix\*(FR%T{
261 disable common and GNU extensions.
262 Enable \*(FIinterval expressions\*(FR in regular
263 expression matching (see \fHRegular
264 Expressions\fP below).
265 T}
266 \*(FC\-\^\-re\-interval\*(FR
267 \*(FC\-W re\-interval\*(FR%T{
268 enable \*(FIinterval expressions\*(FR in regular
269 expression matching (see \fHRegular
270 Expressions\fP below). Useful if
271 \*(FC\-\^\-posix\*(FR is not specified.
272 T}
273 \*(FC\-\^\-source '\*(FItext\*(FC'\*(FR
274 \*(FC\-W source '\*(FItext\*(FC'\*(FR%use \*(FItext\*(FR as AWK program source code.
275 \*(FC\-\^\-version\*(FR
276 \*(FC\-W version\*(FR%T{
277 print version information on \*(FCstdout\fP
278 and exit zero.
279 T}
280 .TE
281 .sp .5
282 .fi
283 In compatibility mode,
284 any other options are flagged as illegal, but are otherwise ignored.
285 In normal operation, as long as program text has been supplied, unknown
286 options are passed on to the AWK program in
287 \*(FCARGV\*(FR
288 for processing. This is most useful for running AWK
289 programs via the \*(FC#!\*(FR executable interpreter mechanism.\*(CB
290 .EB "\s+2\f(HBCOMMAND LINE ARGUMENTS (\*(GK\f(HB)\*(FR\s0"
291
292 .BT
293
294 .ES
295 .fi
296 \*(CDThe following options are specific to \*(MK.
297 .sp .5
298 .fi
299 .TS
300 expand;
301 l lw(1.8i).
302 \*(FC\-W dump\*(FR      T{
303 print an assembly listing of the program to
304 \*(FCstdout\fP and exit zero.
305 T}
306 \*(FC\-W exec \*(FIfile\*(FR    T{
307 read program text from \*(FIfile\fP. No other
308 options are processed. Useful with \*(FC#!\fP.
309 T}
310 \*(FC\-W interactive\*(FR       T{
311 unbuffer \*(FCstdout\fP and line buffer \*(FCstdin\fP.
312 Lines are always records, ignoring \*(FCRS\fP
313 T}
314 \*(FC\-W posix_space\*(FR       T{
315 \*(FC\en\*(FR separates fields when \*(FCRS = "\^"\fP.
316 T}
317 \*(FC\-W sprintf=\*(FInum\*(FR  T{
318 adjust the size of \*(MK's internal
319 \*(FCsprintf\*(FR buffer.
320 T}
321 \*(FC\-W version\*(FR   T{
322 print version and copyright on
323 \*(FCstdout\fP and limit information on \*(FCstderr\fP
324 and exit zero.
325 T}
326 .TE
327 .sp .5
328 .fi
329 The options may be abbreviated using just the first letter, e.g.,
330 \*(FC\-We\*(FR,
331 \*(FC\-Wv\*(FR
332 and so on.\*(CB
333 .EB "\s+2\f(HBCOMMAND LINE ARGUMENTS (\*(MK\f(HB)\*(FR\s0"
334
335 .\" --- Awk Program Execution
336 .ES
337 .fi
338 \*(CDAWK programs are a sequence of pattern-action statements
339 and optional function definitions.
340 .sp .5
341         \*(FIpattern\*(FC       { \*(FIaction statements\*(FC }\*(FR
342 .br
343         \*(FCfunction \*(FIname\*(FC(\*(FIparameter list\*(FC) { \*(FIstatements\*(FC }\*(FR
344 .sp .5
345 \*(AK first reads the program source from the
346 \*(FIprog-file\*(FR(s), if specified,
347 \*(CBfrom arguments to \*(FC\-\^\-source\*(FR,\*(CD
348 or from the first non-option argument on the command line.
349 The program text is read as if all the \*(FIprog-file\*(FR(s)
350 \*(CBand command line
351 source texts\*(CD had been concatenated.
352 .sp .5
353 AWK programs execute in the following order.
354 First, all variable assignments specified via the \*(FC\-v\fP
355 option are performed.
356 Next, \*(AK executes the code in the
357 \*(FCBEGIN\fP rules(s), if any, and then proceeds to read
358 the files \*(FC1\fP through \*(FCARGC \- 1\fP in the \*(FCARGV\fP array.
359 (Adjusting \*(FCARGC\fP and \*(FCARGV\fP thus provides control over
360 the input files that will be processed.)
361 If there are no files named on the command line,
362 \*(AK reads the standard input.
363 .sp .5
364 If a command line argument has the form
365 \*(FIvar\*(FC=\*(FIval\*(FR,
366 it is treated as a variable assignment. The variable
367 \*(FIvar\fP will be assigned the value \*(FIval\*(FR.
368 (This happens after any \*(FCBEGIN\fP rule(s) have been run.)
369 ... delete this paragraph if no space
370 Command line variable assignment
371 is most useful for dynamically assigning values to the variables
372 \*(AK uses to control how input is broken into fields and records. It
373 is also useful for controlling state if multiple passes are needed over
374 a single data file.
375 .sp .5
376 If the value of a particular element of \*(FCARGV\fP is empty
377 (\*(FC"\^"\*(FR), \*(AK skips over it.
378 .sp .5
379 For each record in the input, \*(AK tests to see if it matches any
380 \*(FIpattern\fP in the AWK program.
381 For each pattern that the record matches, the associated
382 \*(FIaction\fP is executed.
383 The patterns are tested in the order they occur in the program.
384 .sp .5
385 Finally, after all the input is exhausted,
386 \*(AK executes the code in the \*(FCEND\fP rule(s), if any.
387 .sp .5
388 If a program only has a \*(FCBEGIN\fP rule, no input files are processed.
389 If a program only has an \*(FCEND\fP rule, the input will be read.
390 \*(CX
391 .EB "\s+2\f(HBAWK PROGRAM EXECUTION\*(FR\s0"
392
393
394 .BT
395
396 .\" --- Lines And Statements
397 .ES
398 .fi
399 \*(CDAWK is a line oriented language. The pattern comes first, and then the
400 action. Action statements are enclosed in \*(FC{\fP and \*(FC}\*(FR.
401 Either the pattern  or the action may be missing, but
402 not both. If the pattern is missing, the action will be
403 executed for every input record.
404 A missing action is equivalent to
405 .sp .5
406         \*(FC{ print }\fP
407 .sp .5
408 which prints the entire record.
409 .sp .5
410 Comments begin with the \*(FC#\*(FR character, and continue until the
411 end of the line.
412 Normally, a statement ends with a newline, but lines ending in
413 a ``,'',
414 \*(FC{\*(FR,
415 \*(CB\*(FC?\*(FR,
416 \*(FC:\*(FR,\*(CD
417 \*(FC&&\*(FR
418 or
419 \*(FC||\*(FR
420 are automatically continued.
421 Lines ending in \*(FCdo\fP or \*(FCelse\fP
422 also have their statements automatically continued on the following line.
423 In other cases, a line can be continued by ending it with a ``\e'',
424 in which case the newline will be ignored. However, a ``\e'' after a
425 \*(FC#\*(FR is not special.
426 .sp .5
427 Multiple statements may be put on one line by separating them with a ``;''.
428 This applies to both the statements within the action part of a
429 pattern-action pair (the usual case)
430 and to the pattern-action statements themselves.\*(CX
431 .EB "\s+2\f(HBLINES AND STATEMENTS\*(FR\s0"
432
433
434
435 .\" --- Regular Expressions
436 .ES
437 .fi
438 \*(CDRegular expressions are the extended kind originally defined by
439 \*(FCegrep\fP.
440 \*(CBAdditional GNU regexp operators are supported by \*(GK.
441 A \*(FIword-constituent\fP character is a letter, digit, or
442 underscore (\*(FC_\fP).\*(CD
443 .sp .5
444 .TS
445 center, tab(~);
446 cp8 sp8
447 cp8 sp8
448 lp8|lp8.
449 .\" .vs 10
450 _
451 Summary of Regular Expressions
452 In Decreasing Precedence
453 _
454 \*(FC(\^\*(FIr\*(FC)\*(FR~regular expression (for grouping)
455 \*(FIc\*(FR~if non-special char, matches itself
456 \*(FC\e\*(FI\^c\*(FR~turn off special meaning of \*(FIc\fP
457 \*(FC^\*(FR~beginning of string (note: \*(FInot\fP line)
458 \*(FC$\*(FR~end of string (note: \*(FInot\fP line)
459 \*(FC.\*(FR~any single character, including newline
460 \*(FC[\*(FR...\*(FC]\*(FR~any one character in ... or range
461 \*(FC[^\*(FR...\*(FC]\*(FR~any one character not in ... or range
462 \*(CB\*(FC\ey\*(FR~word boundary
463 \*(FC\eB\*(FR~middle of a word
464 \*(FC\e<\*(FR~beginning of a word
465 \*(FC\e>\*(FR~end of a word
466 \*(FC\ew\*(FR~any word-constituent character
467 \*(FC\eW\*(FR~any non-word-constituent character
468 \*(FC\e`\*(FR~beginning of a buffer (string)
469 \*(FC\e'\*(FR~end of a buffer (string)\*(CD
470 \*(FIr\*(FC*\*(FR~zero or more occurrences of \*(FIr\*(FR
471 \*(FIr\*(FC+\*(FR~one or more occurrences of \*(FIr\*(FR
472 \*(FIr\*(FC?\*(FR~zero or one occurrences of \*(FIr\*(FR
473 \*(FIr\*(FC{\*(FIn\*(FC,\*(FIm\*(FC}\*(FR~\*(FIn\fP to \*(FIm\fP occurrences of \*(FIr\*(FR \*(CR(POSIX: see note below)\*(CD
474 \*(FIr1\*(FC|\|\*(FIr2\*(FR~\*(FIr1\*(FR or \*(FIr2\*(FR
475 .TE
476 .sp .5
477 .fi
478 \*(CRThe \*(FIr\*(FC{\*(FIn\*(FC,\*(FIm\*(FC}\*(FR notation is called an
479 \*(FIinterval expression\fP. POSIX mandates it for AWK regexps, but
480 most \*(AKs don't implement it. \*(CBUse \*(FC\-\^\-re\-interval\*(FR
481 or \*(FC\-\^\-posix\*(FR to enable
482 this feature in \*(GK.\*(CX
483 .EB "\s+2\f(HBREGULAR EXPRESSIONS\*(FR\s0"
484
485
486 .BT
487
488 .\" --- POSIX Character Classes (gawk)
489 .ES
490 .fi
491 \*(CDIn regular expressions, within character ranges
492 (\*(FC[\*(FR...\*(FC]\*(FR),
493 the notation \*(FC[[:\*(FIclass\*(FC:]]\*(FR defines characters classes:
494 .sp .5
495 .TS
496 center, tab(~);
497 lp8 lp8 lp8 lp8.
498 \*(FCalnum\*(FR~alphanumeric~\*(FClower\*(FR~lower-case
499 \*(FCalpha\*(FR~alphabetic~\*(FCprint\*(FR~printable
500 \*(FCblank\*(FR~space or tab~\*(FCpunct\*(FR~punctuation
501 \*(FCcntrl\*(FR~control~\*(FCspace\*(FR~whitespace
502 \*(FCdigit\*(FR~decimal~\*(FCupper\*(FR~upper-case
503 \*(FCgraph\*(FR~non-spaces~\*(FCxdigit\*(FR~hexadecimal\*(CB
504 .TE
505 .fi
506 .EB "\s+2\f(HBPOSIX CHARACTER CLASSES (\*(GK\f(HB)\*(FR\s0"
507
508 .\" --- Records
509 .ES
510 .fi
511 \*(CDNormally, records are separated by newline characters.
512 Assigning values to the built-in variable \*(FCRS\*(FR
513 controls how records are separated.
514 If \*(FCRS\fP is any single character, that character separates records.
515 \*(CLOtherwise, \*(FCRS\fP is a regular expression.
516 \*(CR(Not \*(NK.)\*(CL
517 Text in the input that matches this
518 regular expression will separate the record.
519 \*(CB\*(GK sets \*(FCRT\*(FR to the value of the
520 input text that matched the regular expression.
521 The value of \*(FCIGNORECASE\fP
522 will also affect how records are separated when
523 \*(FCRS\fP is a regular expression.\*(CD
524 If \*(FCRS\fP is set to the null string,
525 then records are separated by one or more blank lines.
526 When \*(FCRS\fP is set to the null string,
527 the newline character always acts as
528 a field separator, in addition to whatever value
529 \*(FCFS\fP may have.
530 \*(CB\*(MK does not apply exceptional rules to \*(FCFS\fP
531 when \*(FCRS = "\^"\fP.\*(CX
532 .EB \s+2\f(HBRECORDS\*(FR\s0
533
534 .\" --- Fields
535 .ES
536 .fi
537 \*(CDAs each input record is read, \*(AK splits the record into
538 \*(FIfields\*(FR, using the value of the \*(FCFS\fP
539 variable as the field separator.
540 If \*(FCFS\fP is a single character,
541 fields are separated by that character.
542 \*(CLIf \*(FCFS\fP is the null string,
543 then each individual character becomes a separate field.\*(CD
544 Otherwise, \*(FCFS\fP is expected to be a full regular expression.
545 In the special case that \*(FCFS\fP
546 is a single space, fields are separated
547 by runs of spaces and/or tabs
548 \*(CLand/or newlines\*(CD.
549 Leading and trailing whitespace are ignored.
550 \*(CBThe value of \*(FCIGNORECASE\fP
551 will also affect how fields are split when
552 \*(FCFS\fP is a regular expression.\*(CD
553 .sp .5
554 \*(CBIf the \*(FCFIELDWIDTHS\fP
555 variable is set to a space separated list of numbers, each field is
556 expected to have a fixed width, and \*(GK
557 will split up the record using the specified widths.
558 The value of \*(FCFS\fP is ignored.
559 Assigning a new value to \*(FCFS\fP
560 overrides the use of \*(FCFIELDWIDTHS\*(FR,
561 and restores the default behavior.\*(CD
562 .sp .5
563 Each field in the input record may be referenced by its position,
564 \*(FC$1\*(FR, \*(FC$2\*(FR and so on.
565 \*(FC$0\fP is the whole record.
566 Fields may also be assigned new values.
567 .sp .5
568 The variable \*(FCNF\fP
569 is set to the total number of fields in the input record.
570 .sp .5
571 References to non-existent fields (i.e., fields after \*(FC$NF\*(FR)
572 produce the null-string. However, assigning to a non-existent field
573 (e.g., \*(FC$(NF+2) = 5\*(FR) will increase the value of
574 \*(FCNF\*(FR, create any intervening fields with the null string as their value,
575 and cause the value of \*(FC$0\fP
576 to be recomputed with the fields being separated by the
577 value of \*(FCOFS\*(FR.
578 References to negative numbered fields cause a fatal error.
579 Decreasing the value of \*(FCNF\fP causes the trailing fields to be lost
580 \*(CR(not \*(NK).\*(CX
581 .EB \s+2\f(HBFIELDS\*(FR\s0
582
583 .BT
584
585 .\" --- Pattern Elements
586 .ES
587 .fi
588 \*(CDAWK patterns may be one of the following.
589 .sp .5
590 .nf
591         \*(FCBEGIN
592         END
593         \*(FIexpression 
594         pat1\*(FC,\*(FIpat2\*(FR
595 .sp .5
596 .fi
597 \*(FCBEGIN\fP and \*(FCEND\fP are special patterns that provide start-up
598 and clean-up actions respectively. They must have actions. There can
599 be multiple \*(FCBEGIN\fP and \*(FCEND\fP rules; they are merged and
600 executed as if there had just been one large rule. They may occur anywhere
601 in a program, including different source files.
602 .sp .5
603 Expression patterns can be any expression, as described
604 under \fHExpressions\fP.
605 .sp .5
606 The \*(FIpat1\*(FC,\*(FIpat2\*(FR pattern
607 is called a \*(FIrange pattern\*(FR.
608 It matches all input records starting with a record that matches
609 \*(FIpat1\*(FR, and continuing until a record that matches
610 \*(FIpat2\*(FR, inclusive.
611 It does not combine with any other pattern expression.\*(CX
612 .EB "\s+2\f(HBPATTERN ELEMENTS\*(FR\s0"
613
614
615 .\" --- Action Statements
616 .ES
617 .nf
618 \*(CD\*(FCif (\*(FIcondition\*(FC) \*(FIstatement\*(FR [ \*(FCelse\*(FI statement \*(FR]
619 \*(FCwhile (\*(FIcondition\*(FC) \*(FIstatement \*(FR
620 \*(FCdo \*(FIstatement  \*(FCwhile (\*(FIcondition\*(FC)\*(FR
621 \*(FCfor (\*(FIexpr1\*(FC; \*(FIexpr2\*(FC; \*(FIexpr3\*(FC) \*(FIstatement\*(FR
622 \*(FCfor (\*(FIvar \*(FCin\*(FI array\*(FC) \*(FIstatement\*(FR
623 .ig
624 \*(CB\*(FCabort\*(FR [ \*(FIexpression\*(FR ]\*(CD
625 ..
626 \*(FCbreak\*(FR
627 \*(FCcontinue\*(FR
628 \*(FCdelete \*(FIarray\^\*(FC[\^\*(FIindex\^\*(FC]\*(FR
629 \*(CL\*(FCdelete \*(FIarray\^\*(FR\*(CD
630 \*(FCexit\*(FR [ \*(FIexpression\*(FR ]
631 \*(FCnext\*(FR
632 \*(CL\*(FCnextfile\*(FR \*(CR(not \*(MK)\*(CD
633 \*(FC{ \*(FIstatements \*(FC}\*(CX
634 .EB "\s+2\f(HBACTION STATEMENTS\*(FR\s0"
635
636
637
638 .\" --- Escape Sequences
639 .ES
640 .fi
641 \*(CDWithin strings constants (\*(FC"..."\fP) and regexp
642 constants (\*(FC/.../\fP), escape sequences may be used to
643 generate otherwise unprintable characters. This table lists
644 the available escape sequences.
645 .sp .5
646 .ig
647 \*(CB\*(FCPROCINFO\fP   T{
648 elements of this array provide access to info
649 about the running AWK program. See
650 \*(AM for details.\*(CD
651 T}
652 ..
653 .TS
654 center, tab(~);
655 lp8 lp8 lp8 lp8.
656 \*(FC\ea\fP~alert (bell)~\*(FC\er\fP~carriage return
657 \*(FC\eb\fP~backspace~\*(FC\et\fP~horizontal tab
658 \*(FC\ef\fP~form feed~\*(FC\ev\fP~vertical tab
659 \*(FC\en\fP~newline~\*(FC\e\e\fP~backslash
660 \*(FC\e\*(FIddd\*(FR~octal value \*(FIddd\fP~\*(CL\*(FC\ex\*(FIhh\*(FR~hex value \*(FIhh\fP\*(CD
661 \*(FC\e"\fP~double quote~\*(FC\e/\fP~forward slash\*(CX
662 .TE
663 .EB "\s+2\f(HBESCAPE SEQUENCES\*(FR\s0"
664
665
666 .BT
667
668 .\" --- Variables
669 .ES
670 .fi
671 .TS
672 expand;
673 l lw(2i).
674 \*(FCARGC\fP    T{
675 number of command line arguments.
676 T}
677 \*(CB\*(FCARGIND\fP     T{
678 index in \*(FCARGV\fP of current data file.\*(CD
679 T}
680 \*(FCARGV\fP    T{
681 array of command line arguments. Indexed from
682 0 to \*(FCARGC\fP \- 1. Dynamically changing the
683 contents of \*(FCARGV\fP can control the files used
684 for data.
685 T}
686 \*(FCCONVFMT\fP T{
687 conversion format for numbers, default value
688 is \*(FC"%.6g"\*(FR.
689 T}
690 \*(FCENVIRON\fP T{
691 array containing the the current environment.
692 The array is indexed by the environment
693 variables, each element being the value of
694 that variable.
695 T}
696 \*(CB\*(FCERRNO\fP      T{
697 contains a string describing the error when a
698 redirection or read for \*(FCgetline\*(FR fails, or if
699 \*(FCclose()\*(FR fails.
700 T}
701 \*(FCFIELDWIDTHS\fP     T{
702 white-space separated list of fieldwidths. Used
703 to parse the input into fields of fixed width,
704 instead of the value of \*(FCFS\fP.\*(CD
705 T}
706 \*(FCFILENAME\fP        T{
707 name of the current input file. If no files given
708 on the command line, \*(FCFILENAME\fP is ``\-''.
709 \*(FCFILENAME\fP is undefined inside the \*(FCBEGIN\fP rule
710 (unless set by \*(FCgetline\fP).
711 T}
712 \*(FCFNR\fP     T{
713 number of the input record in current input file.
714 T}
715 \*(FCFS\fP      T{
716 input field separator, a space by default
717 (see \fHFields\fP above).
718 T}
719 \*(CB\*(FCIGNORECASE\fP T{
720 if non-zero, all regular expression and string
721 operations ignore case. \*(CRIn versions of \*(GK
722 prior to 3.0, \*(FCIGNORECASE\fP only affected
723 regular expression operations and \*(FCindex()\*(FR.\*(CD
724 T}
725 \*(FCNF\fP      T{
726 number of fields in the current input record.
727 T}
728 \*(FCNR\fP      T{
729 total number of input records seen so far.
730 T}
731 \*(FCOFMT\fP    T{
732 output format for numbers, \*(FC"%.6g"\*(FR, by default.
733 \*(CROld versions of \*(AK also used this for number
734 to string conversion instead of \*(FCCONVFMT\fP.\*(CD
735 T}
736 \*(FCOFS\fP     T{
737 output field separator, a space by default.
738 T}
739 \*(FCORS\fP     T{
740 output record separator, a newline by default.
741 T}
742 \*(FCRS\fP      T{
743 input record separator, a newline by default
744 (see \fHRecords\fP above).
745 T}
746 \*(CB\*(FCRT\fP T{
747 record terminator. \*(GK sets \*(FCRT\fP to the input
748 text that matched the character or regular
749 expression specified by \*(FCRS\*(FR.\*(CD
750 T}
751 \*(FCRSTART\fP  T{
752 index of the first character matched by
753 \*(FCmatch()\*(FR; 0 if no match.
754 T}
755 \*(FCRLENGTH\fP T{
756 length of the string matched by \*(FCmatch()\*(FR;
757 \-1 if no match.
758 T}
759 \*(FCSUBSEP\fP  T{
760 character(s) used to separate multiple subscripts
761 in array elements, by default \*(FC"\e034"\*(FR. (see
762 \fHArrays\fP below).\*(CX
763 T}
764 .TE
765 .EB \s+2\f(HBVARIABLES\*(FR\s0
766
767 .BT
768
769 .\" --- Arrays
770 .ES
771 .fi
772 \*(CDAn arrays subscript is an expression between square brackets
773 (\*(FC[ \*(FRand \*(FC]\*(FR).
774 If the expression is a list
775 \*(FC(\*(FIexpr\*(FC, \*(FIexpr \*(FC...)\*(FR,
776 then the subscript is a string consisting of the
777 concatenation of the (string) value of each expression,
778 separated by the value of the \*(FCSUBSEP\fP variable.
779 This simulates multi-dimensional
780 arrays. For example:
781 .nf
782 .sp .5
783         \*(FCi = "A";\^ j = "B";\^ k = "C"
784         x[i, j, k] = "hello, world\en"\*(FR
785 .sp .5
786 .fi
787 assigns \*(FC"hello, world\en"\*(FR to the element of the array
788 \*(FCx\fP
789 indexed by the string \*(FC"A\e034B\e034C"\*(FR. All arrays in AWK
790 are associative, i.e., indexed by string values.
791 .sp .5
792 Use the special operator \*(FCin\fP in an \*(FCif\fP
793 or \*(FCwhile\fP statement to see if a particular value is
794 an array index.
795 .sp .5
796 .nf
797         \*(FCif (val in array)
798                 print array[val]\*(FR
799 .sp .5
800 .fi
801 If the array has multiple subscripts, use
802 \*(FC(i, j) in array\*(FR.
803 .sp .5
804 Use the \*(FCin\fP construct in a \*(FCfor\fP
805 loop to iterate over all the elements of an array.
806 .sp .5
807 Use the \*(FCdelete\fP statement to delete an
808 element from an array.
809 \*(CLSpecifying just the array name without a subscript in
810 the \*(FCdelete\fP
811 statement deletes the entire contents of an array.\*(CX
812 .EB \s+2\f(HBARRAYS\*(FR\s0
813
814 .\" --- Expressions
815 .ES
816 .fi
817 \*(CDExpressions are used as patterns, for controlling conditional action
818 statements, and to produce parameter values when calling functions.
819 Expressions may also be used as simple statements,
820 particularly if they have side-effects such as assignment.
821 Expressions mix \*(FIoperands\fP and \*(FIoperators\fP. Operands are
822 constants, fields, variables, array elements, and the return
823 values from function calls (both built-in and user-defined).
824 .sp .5
825 Regexp constants (\*(FC/\*(FIpat\*(FC/\*(FR), when used as simple expressions,
826 i.e., not used on the right-hand side of
827 \*(FC~\fP and \*(FC!~\fP, or as arguments to the
828 \*(CB\*(FCgensub()\fP,\*(CD
829 \*(FCgsub()\fP,
830 \*(FCmatch()\fP,
831 \*(FCsplit()\fP,
832 and
833 \*(FCsub()\fP,
834 functions, mean \*(FC$0 ~ /\*(FIpat\*(FC/\*(FR.
835 .sp .5
836 The AWK operators, in order of decreasing precedence, are
837 .sp .5
838 .fi
839 .TS
840 expand;
841 l lw(1.8i).
842 \*(FC(\&...)\*(FR       grouping
843 \*(FC$\fP       field reference
844 \*(FC++ \-\^\-\fP       T{
845 increment and decrement,
846 prefix and postfix
847 T}
848 \*(FC^\fP \*(CL\*(FC**\*(FR\*(CD        exponentiation
849 \*(FC+ \- !\fP  unary plus, unary minus, and logical negation
850 \*(FC* / %\fP   multiplication, division, and modulus
851 \*(FC+ \-\fP    addition and subtraction
852 \*(FIspace\fP   string concatenation
853 \*(FC< >\fP     less than, greater than
854 \*(FC<= >=\fP   less than or equal, greater than or equal
855 \*(FC!= ==\fP   not equal, equal
856 \*(FC~ !~\fP    regular expression match, negated match
857 \*(FCin\fP      array membership
858 \*(FC&&\fP      logical AND, short circuit
859 \*(FC||\fP      logical OR, short circuit
860 \*(FC?\^:\fP    in-line conditional expression
861 .T&
862 l s
863 l lw(1.8i).
864 \*(FC=\0+=\0\-=\0*=\0/=\0%=\0^=\0\*(CL**=\*(CD\fP
865         assignment operators\*(CX
866 .TE
867 .EB \s+2\f(HBEXPRESSIONS\*(FR\s0
868
869
870 .BT
871
872 .\" --- Conversions and Comparisons
873 .ES
874 .fi
875 \*(CDVariables and fields may be (floating point) numbers, strings or both.
876 Context determines how the value of a variable is interpreted. If used in
877 a numeric expression, it will be treated as a number, if used as a string
878 it will be treated as a string.
879 .sp .5
880 To force a variable to be treated as a number, add 0 to it; to force it
881 to be treated as a string, concatenate it with the null string.
882 .sp .5
883 When a string must be converted to a number, the conversion is accomplished
884 using \*(FIatof\*(FR(3).
885 A number is converted to a string by using the value of \*(FCCONVFMT\fP
886 as a format string for \*(FIsprintf\*(FR(3),
887 with the numeric value of the variable as the argument.
888 However, even though all numbers in AWK are floating-point,
889 integral values are \*(FIalways\fP converted as integers.
890 .sp .5
891 Comparisons are performed as follows:
892 If two variables are numeric, they are compared numerically.
893 If one value is numeric and the other has a string value that is a
894 ``numeric string,'' then comparisons are also done numerically.
895 Otherwise, the numeric value is converted to a string, and a string
896 comparison is performed.
897 Two strings are compared, of course, as strings.
898 \*(CRAccording to the POSIX standard, even if two strings are
899 numeric strings, a numeric comparison is performed. However, this is
900 clearly incorrect, and none of the three free \*(AK\*(FRs do this.\*(CD
901 .sp .5
902 Note that string constants, such as \*(FC"57"\fP, are \*(FInot\fP
903 numeric strings, they are string constants. The idea of ``numeric string''
904 only applies to fields, \*(FCgetline\fP input,
905 \*(FCFILENAME\*(FR, \*(FCARGV\fP elements, \*(FCENVIRON\fP
906 elements and the elements of an array created by
907 \*(FCsplit()\fP that are numeric strings.
908 The basic idea is that \*(FIuser input\*(FR,
909 and only user input, that looks numeric,
910 should be treated that way.
911 .sp .5
912 Uninitialized variables have the numeric value 0 and the string value
913 \*(FC"\^"\fP
914 (the null, or empty, string).\*(CX
915 .EB "\s+2\f(HBCONVERSIONS AND COMPARISONS\*(FR\s0"
916
917 .ig
918 .\" --- Localization
919 .ES
920 .nf
921 .ce 100
922 \*(CDThis
923 section
924 is
925 under
926 construction.
927 .sp .5
928 This
929 section
930 is
931 under
932 construction.\*(CB
933 .ce 0
934 .EB "\s+2\f(HBLOCALIZATION\*(FR\s0"
935 ..
936
937 .ig
938 .ps +2
939 .ce 1
940 \*(CD\fHISBN: 0-916151-97-2\*(FR
941 .ps -2
942 ..
943
944 .BT
945
946
947 .\" --- Input Control
948 .ES
949 .fi
950 .TS
951 expand;
952 l lw(1.8i).
953 \*(CD\*(FCclose(\*(FIfile\*(FC)\*(FR    close input file or pipe.
954 \*(FCgetline\fP T{
955 set \*(FC$0\fP from next input record;
956 set \*(FCNF\*(FR, \*(FCNR\*(FR, \*(FCFNR\*(FR.
957 T}
958 \*(FCgetline < \*(FIfile\*(FR   set \*(FC$0\fP from next record of \*(FIfile\*(FR; set \*(FCNF\*(FR.
959 \*(FCgetline \*(FIv\*(FR        T{
960 set \*(FIv\fP from next input record;
961 set \*(FCNR\*(FR, \*(FCFNR\*(FR.
962 T}
963 \*(FCgetline \*(FIv \*(FC< \*(FIfile\*(FR       set \*(FIv\fP from next record of \*(FIfile\*(FR.
964 \*(FIcmd \*(FC| getline\*(FR    pipe into \*(FCgetline\*(FR; set \*(FC$0\*(FR, \*(FCNF\*(FR.
965 \*(FIcmd \*(FC| getline \*(FIv\*(FR     pipe into \*(FCgetline\*(FR; set \*(FIv\*(FR.
966 .TE
967 .fi
968 .in +.2i
969 .ti -.2i
970 \*(FCnext\fP
971 .br
972 stop processing the current input
973 record. Read next input record and
974 start over with the first pattern in the
975 program. Upon end of the input data,
976 execute any \*(FCEND\fP rule(s).
977 .br
978 .ti -.2i
979 \*(CL\*(FCnextfile\fP
980 .br
981 stop processing the current input file.
982 The next input record comes from the
983 next input file. \*(FCFILENAME\fP \*(CBand
984 \*(FCARGIND\fP\*(CL are updated, \*(FCFNR\fP is reset to 1,
985 and processing starts over with the first
986 pattern in the AWK program. Upon end
987 of input data, execute any \*(FCEND\fP rule(s).
988 \*(CREarlier versions of \*(GK used
989 \*(FCnext file\*(FR, as two words. This
990 generates a warning message and will
991 eventually be removed. \*(CR\*(MK does not
992 currently support \*(FCnextfile\*(FR.\*(CD
993 .in -.2i
994 .sp .5
995 .fi
996 \*(FCgetline\*(FR returns 0 on end of file, and \-1 on an
997 error.\*(CX
998 .EB "\s+2\f(HBINPUT CONTROL\*(FR\s0"
999
1000 .\" --- Output Control
1001 .ES
1002 .fi
1003 .in +.2i
1004 .ti -.2i
1005 \*(CD\*(FCclose(\*(FIfile\*(FC)\*(FR
1006 .br
1007 close output file or pipe.
1008 .ti -.2i
1009 \*(CL\*(FCfflush(\*(FR[\*(FIfile\^\*(FR]\*(FC)\*(FR
1010 .br
1011 flush any buffers associated
1012 with the open output file or pipe \*(FIfile\*(FR.\*(CD
1013 \*(CBIf \*(FIfile\fP is missing, then standard output is flushed.
1014 If \*(FIfile\fP is the null string, then all open output files and pipes
1015 are flushed \*(CR(not \*(NK)\*(CD.
1016 .ti -.2i
1017 \*(FCprint\fP
1018 .br
1019 print the current record. The output record is terminated
1020 with the value of \*(FCORS\fP.
1021 .ti -.2i
1022 \*(FCprint \*(FIexpr-list\*(FR
1023 .br
1024 print expressions. Each expression is separated
1025 by the value of \*(FCOFS\fP. The output record is
1026 terminated with the value of \*(FCORS\fP.
1027 .ti -.2i
1028 \*(FCprintf \*(FIfmt\*(FC, \*(FIexpr-list\*(FR
1029 .br
1030 format and print (see \fHPrintf Formats\fP below).
1031 .ti -.2i
1032 \*(FCsystem(\*(FIcmd\*(FC)\*(FR
1033 .br
1034 execute the command \*(FIcmd\*(FR,
1035 and return the exit status
1036 \*(CR(may not be available on non-POSIX systems)\*(CD.
1037 .sp .5
1038 .in -.2i
1039 I/O redirections may be used with both \*(FCprint\fP and \*(FCprintf\fP.
1040 .sp .5
1041 .in +.2i
1042 .ti -.2i
1043 \*(CD\*(FCprint "hello" > \*(FIfile\*(FR
1044 .br
1045 Print data to \*(FIfile\fP. The first time the file is written to, it
1046 will be truncated. Subsequent commands append data.
1047 .ti -.2i
1048 \*(FCprint "hello" >> \*(FIfile\*(FR
1049 .br
1050 Append data to \*(FIfile\fP. The previous contents of the file are not lost.
1051 .ti -.2i
1052 \*(FCprint "hello" | \*(FIcmd\*(FR
1053 .br
1054 Print data down a pipeline to \*(FIcmd\*(FR.\*(CX
1055 .in -.2i
1056 .EB "\s+2\f(HBOUTPUT CONTROL\*(FR\s0"
1057
1058 .BT
1059
1060
1061 .\" --- Printf Formats
1062 .ES
1063 .fi
1064 \*(CDThe \*(FCprintf\fP statement and
1065 \*(FCsprintf()\fP function
1066 accept the following conversion specification formats:
1067 .sp .5
1068 .nf
1069 \*(FC%c\fP              an \s-1ASCII\s+1 character
1070 \*(FC%d\fP              a decimal number (the integer part)
1071 \*(FC%i\fP              a decimal number (the integer part)
1072 \*(FC%e\fP              a floating point number of the form
1073                 \*(FC[\-]d.dddddde[+\^\-]dd\*(FR
1074 \*(FC%E\fP              like \*(FC%e\fP, but use \*(FCE\fP instead of \*(FCe\*(FR
1075 \*(FC%f\fP              a floating point number of the form
1076                 \*(FC[\-]ddd.dddddd\*(FR
1077 \*(FC%g\fP              use \*(FC%e\fP or \*(FC%f\fP, whichever is shorter, with
1078                 nonsignificant zeros suppressed
1079 \*(FC%G\fP              like \*(FC%g\fP, but use \*(FC%E\fP instead of \*(FC%e\*(FR
1080 \*(FC%o\fP              an unsigned octal integer
1081 \*(FC%u\fP              an unsigned decimal integer
1082 \*(FC%s\fP              a character string
1083 \*(FC%x\fP              an unsigned hexadecimal integer
1084 \*(FC%X\fP              like \*(FC%x\fP, but use \*(FCABCDEF\fP for 10\(en15
1085 \*(FC%%\fP              A literal \*(FC%\fP; no argument is converted
1086 .sp .5
1087 .fi
1088 Optional, additional parameters may lie between the \*(FC%\fP
1089 and the control letter:
1090 .sp .5
1091 .TS
1092 expand;
1093 l lw(2.2i).
1094 \*(FC\-\fP      T{
1095 left-justify the expression within its field.
1096 T}
1097 \*(FIspace\fP   T{
1098 for numeric conversions, prefix positive values
1099 with a space and negative values with a
1100 minus sign.
1101 T}
1102 \*(FC+\fP       T{
1103 used before the \*(FIwidth\fP modifier means to always
1104 supply a sign for numeric conversions, even if
1105 the data to be formatted is positive. The \*(FC+\fP
1106 overrides the space modifier.
1107 T}
1108 \*(FC#\fP       T{
1109 use an ``alternate form'' for some control letters.
1110 T}
1111  \*(FC%o\*(FR   T{
1112 supply a leading zero.
1113 T}
1114  \*(FC%x\*(FR, \*(FC%X\*(FR     T{
1115 supply a leading \*(FC0x\*(FR or \*(FC0X\*(FR for a nonzero result.
1116 T}
1117  \*(FC%e\*(FR, \*(FC%E\*(FR, \*(FC%f\*(FR       T{
1118 the result always has a decimal point.
1119 T}
1120  \*(FC%g\*(FR, \*(FC%G\*(FR     T{
1121 trailing zeros are not removed.
1122 T}
1123 \*(FC0\fP       T{
1124 a leading zero acts as a flag, indicating output
1125 should be padded with zeroes instead of spaces.
1126 This applies even to non-numeric output formats.
1127 Only has an effect when the field width is wider
1128 than the value to be printed.
1129 T}
1130 \*(FIwidth\fP   T{
1131 pad the field to this width. The field is normally
1132 padded with spaces. If the \*(FC0\fP flag has been used,
1133 pad with zeroes.
1134 T}
1135 \*(FC.\fP\*(FIprec\fP   T{
1136 precision.
1137 The meaning varies by control letter:
1138 T}
1139  \*(FC%d\*(FR, \*(FC%o\*(FR, \*(FC%i\*(FR,
1140  \*(FC%u\*(FR, \*(FC%x\*(FR, \*(FC%X\fP T{
1141 the minimum number of digits to print.
1142 T}
1143  \*(FC%e\*(FR, \*(FC%E\*(FR, \*(FC%f\*(FR       T{
1144 the number of digits to print to the right of the decimal point.
1145 T}
1146  \*(FC%g\*(FR, \*(FC%G\fP       T{
1147 the maximum number of significant digits.
1148 T}
1149  \*(FC%s\fP     T{
1150 the maximum number of characters to print.
1151 T}
1152 .TE
1153 .sp .5
1154 .fi
1155 The dynamic \*(FIwidth\fP and \*(FIprec\fP capabilities of the ANSI C
1156 \*(FCprintf()\fP routines are supported.
1157 A \*(FC*\fP in place of either the \*(FIwidth\fP or \*(FIprec\fP
1158 specifications will cause their values to be taken from
1159 the argument list to \*(FCprintf\fP or \*(FCsprintf()\*(FR.\*(CX
1160 .EB "\s+2\f(HBPRINTF FORMATS\*(FR\s0"
1161
1162
1163
1164 .BT
1165
1166 .\" --- Special Filenames
1167 .ES
1168 .fi
1169 \*(CDWhen doing I/O redirection from either \*(FCprint\fP
1170 or \*(FCprintf\fP into a file or via \*(FCgetline\fP
1171 from a file, all three implementations of \*(FCawk\fP
1172 recognize certain special filenames internally. These filenames
1173 allow access to open file descriptors inherited from the
1174 parent process (usually the shell).
1175 These filenames may also be used on the command line to name data files.
1176 The filenames are:
1177 .sp .5
1178 .TS
1179 expand;
1180 l lw(2i).
1181 \*(FC"\-"\fP    standard input
1182 \*(FC/dev/stdin\fP      standard input \*(CR(not \*(MK)\*(CD
1183 \*(FC/dev/stdout\fP     standard output
1184 \*(FC/dev/stderr\fP     standard error output
1185 .TE
1186 .sp .5
1187 .fi
1188 \*(CBThe following names are specific to \*(GK.
1189 .sp .5
1190 .TS
1191 expand;
1192 l lw(2i).
1193 \*(FC/dev/fd/\^\*(FIn\*(FR      T{
1194 file associated with the open file descriptor \*(FIn\*(FR
1195 T}
1196 .TE
1197 .sp .5
1198 .fi
1199 Other special filenames provide access to information about the running
1200 \*(FCgawk\fP process.
1201 Reading from these files returns a single record.
1202 The filenames and what they return are:\*(FR
1203 .sp .5
1204 .TS
1205 expand;
1206 l lw(2i).
1207 \*(FC/dev/pid\fP        process ID of current process
1208 \*(FC/dev/ppid\fP       parent process ID of current process
1209 \*(FC/dev/pgrpid\fP     process group ID of current process
1210 \*(FC/dev/user\fP       T{
1211 .nf
1212 a single newline-terminated record.
1213 The fields are separated with spaces.
1214 \*(FC$1\fP is the return value of \*(FIgetuid\*(FR(2),
1215 \*(FC$2\fP is the return value of \*(FIgeteuid\*(FR(2),
1216 \*(FC$3\fP is the return value of \*(FIgetgid\*(FR(2) , and
1217 \*(FC$4\fP is the return value of \*(FIgetegid\*(FR(2).
1218 .fi
1219 Any additional fields are the group IDs returned
1220 by \*(FIgetgroups\*(FR(2). Multiple groups may not be
1221 supported on all systems.
1222 T}
1223 .TE
1224 .sp .5
1225 .fi
1226 .ig
1227 \*(CRThese filenames are now obsolete.
1228 Use the \*(FCPROCINFO\fP array to obtain the information they provide.\*(CL
1229 ..
1230 .\" BEGIN FOR 3.0.x
1231 \*(CRThese filenames will become obsolete in \*(GK 3.1.
1232 Be aware that you will have to change your programs.\*(CL
1233 .\" END FOR 3.0.x
1234 .EB "\s+2\f(HBSPECIAL FILENAMES\*(FR\s0"
1235
1236
1237
1238
1239 .\" --- Builtin Numeric Functions
1240 .ES
1241 .fi
1242 .TS
1243 expand;
1244 l lw(2i).
1245 \*(CD\*(FCatan2(\*(FIy\*(FC, \*(FIx\*(FC)\*(FR  the arctangent of \*(FIy/x\fP in radians.
1246 \*(FCcos(\*(FIexpr\*(FC)\*(FR   the cosine of \*(FIexpr\fP, which is in radians.
1247 \*(FCexp(\*(FIexpr\*(FC)\*(FR   the exponential function (\*(FIe \*(FC^ \*(FIx\*(FR).
1248 \*(FCint(\*(FIexpr\*(FC)\*(FR   truncates to integer.
1249 \*(FClog(\*(FIexpr\*(FC)\*(FR   the natural logarithm function (base \*(FIe\^\*(FR).
1250 \*(FCrand()\fP  a random number between 0 and 1.
1251 \*(FCsin(\*(FIexpr\*(FC)\*(FR   the sine of \*(FIexpr\fP, which is in radians.
1252 \*(FCsqrt(\*(FIexpr\*(FC)\*(FR  the square root function.
1253 \&\*(FCsrand(\*(FR[\*(FIexpr\^\*(FR]\*(FC)\*(FR T{
1254 uses \*(FIexpr\fP as a new seed for the random number
1255 generator. If no \*(FIexpr\fP, the time of day is used.
1256 Returns previous seed for the random number
1257 generator.\*(CX
1258 T}
1259 .TE
1260 .EB "\s+2\f(HBNUMERIC FUNCTIONS\*(FR\s0"
1261
1262
1263 .BT
1264
1265
1266 .\" --- Builtin String Functions
1267 .ES
1268 .fi
1269 .in +.2i
1270 .ti -.2i
1271 \*(CB\*(FCgensub(\*(FIr\*(FC, \*(FIs\*(FC, \*(FIh \*(FR[\*(FC, \*(FIt\*(FR]\*(FC)\*(FR
1272 .br
1273 search the target string
1274 \*(FIt\fP for matches of the regular expression \*(FIr\*(FR. If
1275 \*(FIh\fP is a string beginning with \*(FCg\fP or \*(FCG\*(FR,
1276 replace all matches of \*(FIr\fP with \*(FIs\*(FR. Otherwise, \*(FIh\fP
1277 is a number indicating which match of \*(FIr\fP to replace. If no
1278 \*(FIt\fP is supplied, \*(FC$0\fP is used instead. Within the
1279 replacement text \*(FIs\*(FR, the sequence \*(FC\e\*(FIn\*(FR,
1280 where \*(FIn\fP is a digit from 1 to 9, may be used to indicate just
1281 the text that matched the \*(FIn\*(FRth parenthesized subexpression.
1282 The sequence \*(FC\e0\fP represents the entire matched text, as does
1283 the character \*(FC&\*(FR. Unlike \*(FCsub()\fP and \*(FCgsub()\*(FR,
1284 the modified string is returned as the result of the function,
1285 and the original target string is \*(FInot\fP changed.\*(CD
1286 .ti -.2i
1287 \*(FCgsub(\*(FIr\*(FC, \*(FIs \*(FR[\*(FC, \*(FIt\*(FR]\*(FC)\*(FR
1288 .br
1289 for each substring matching the
1290 regular expression \*(FIr\fP in the string \*(FIt\*(FR, substitute the
1291 string \*(FIs\*(FR, and return the number of substitutions. If
1292 \*(FIt\fP is not supplied, use \*(FC$0\*(FR. An \*(FC&\fP in the
1293 replacement text is replaced with the text that was actually matched.
1294 Use \*(FC\e&\fP to get a literal \*(FC&\*(FR. See \*(AM
1295 for a fuller discussion of the rules for \*(FC&\*(FR's and backslashes
1296 in the replacement text of \*(CB\*(FCgensub()\*(FR,\*(CD \*(FCsub()\*(FR
1297 and \*(FCgsub()\*(FR
1298 .ti -.2i
1299 \*(FCindex(\*(FIs\*(FC, \*(FIt\*(FC)\*(FR
1300 .br
1301 returns the index of the string
1302 \*(FIt\fP in the string \*(FIs\*(FR, or 0 if \*(FIt\fP is not present.
1303 .ti -.2i
1304 \*(FClength(\*(FR[\*(FIs\*(FR]\*(FC)\*(FR
1305 .br
1306 returns the length of the string
1307 \*(FIs\*(FR, or the length of \*(FC$0\fP if \*(FIs\fP is not supplied.
1308 .ti -.2i
1309 \*(FCmatch(\*(FIs\*(FC, \*(FIr\*(FC)\*(FR
1310 .br
1311 returns the position in
1312 \*(FIs\fP where the regular expression \*(FIr\fP occurs, or 0 if
1313 \*(FIr\fP is not present, and sets the values of variables
1314 \*(FCRSTART\fP
1315 and \*(FCRLENGTH\*(FR.
1316 .ti -.2i
1317 \*(FCsplit(\*(FIs\*(FC, \*(FIa \*(FR[\*(FC, \*(FIr\*(FR]\*(FC)\*(FR
1318 .br
1319 splits the string
1320 \*(FIs\fP into the array \*(FIa\fP using the regular expression \*(FIr\*(FR,
1321 and returns the number of fields. If \*(FIr\fP is omitted, \*(FCFS\fP
1322 is used instead. The array \*(FIa\fP is cleared first.
1323 Splitting behaves identically to field splitting.
1324 (See \fHFields\fP, above.)
1325 .ti -.2i
1326 \*(FCsprintf(\*(FIfmt\*(FC, \*(FIexpr-list\*(FC)\*(FR
1327 .br
1328 prints \*(FIexpr-list\fP
1329 according to \*(FIfmt\*(FR, and returns the resulting string.
1330 .ti -.2i
1331 \*(FCsub(\*(FIr\*(FC, \*(FIs \*(FR[\*(FC, \*(FIt\*(FR]\*(FC)\*(FR
1332 .br
1333 just like
1334 \*(FCgsub()\*(FR, but only the first matching substring is replaced.
1335 .ti -.2i
1336 \*(FCsubstr(\*(FIs\*(FC, \*(FIi \*(FR[\*(FC, \*(FIn\*(FR]\*(FC)\*(FR
1337 .br
1338 returns the at most
1339 \*(FIn\*(FR-character substring of \*(FIs\fP starting at \*(FIi\*(FR.
1340 If \*(FIn\fP is omitted, the rest of \*(FIs\fP is used.
1341 .ti -.2i
1342 \*(FCtolower(\*(FIstr\*(FC)\*(FR
1343 .br
1344 returns a copy of the string \*(FIstr\*(FR,
1345 with all the upper-case characters in \*(FIstr\fP translated to their
1346 corresponding lower-case counterparts. Non-alphabetic characters are
1347 left unchanged.
1348 .ti -.2i
1349 \*(FCtoupper(\*(FIstr\*(FC)\*(FR
1350 .br
1351 returns a copy of the string \*(FIstr\*(FR,
1352 with all the lower-case characters in \*(FIstr\fP translated to their
1353 corresponding upper-case counterparts. Non-alphabetic characters are
1354 left unchanged.\*(CX
1355 .in -.2i
1356 .EB "\s+2\f(HBSTRING FUNCTIONS\*(FR\s0"
1357
1358
1359
1360 .BT
1361
1362
1363 .\" --- Builtin Time Functions
1364 .ES
1365 .fi
1366 \*(CD\*(GK
1367 provides the following functions for obtaining time stamps and
1368 formatting them.
1369 .sp .5
1370 .fi
1371 .in +.2i
1372 .ig
1373 .ti -.2i
1374 \*(FCmktime(\*(FIdatespec\*(FC)\*(FR
1375 .br
1376 turns \*(FIdatespec\fP into a time
1377 stamp of the same form as returned by \*(FCsystime()\*(FR.
1378 The \*(FIdatespec\fP is a string of the form
1379 \*(FC"\*(FIYYYY MM DD HH MM SS\*(FC"\*(FR.
1380 ..
1381 .ti -.2i
1382 \*(FCstrftime(\*(FR[\*(FIformat \*(FR[\*(FC, \*(FItimestamp\*(FR]]\*(FC)\*(FR
1383 .br
1384 formats \*(FItimestamp\fP
1385 according to the specification in \*(FIformat\*(FR. The
1386 \*(FItimestamp\fP should be of the same form as returned by
1387 \*(FCsystime()\*(FR.
1388 If \*(FItimestamp\fP is missing, the current time of day is used. If
1389 \*(FIformat\fP is missing, a default format equivalent to the output
1390 of \*(FIdate\*(FR(1) will be used.
1391 .ti -.2i
1392 \*(FCsystime()\fP
1393 .br
1394 returns the current time of day as the number of
1395 seconds since the Epoch.\*(CB
1396 .in -.2i
1397 .EB "\s+2\f(HBTIME FUNCTIONS (\*(GK\f(HB)\*(FR\s0"
1398
1399
1400
1401 .\" --- User-defined Functions
1402 .ES
1403 .fi
1404 \*(CDFunctions in AWK are defined as follows:
1405 .sp .5
1406 .nf
1407         \*(FCfunction \*(FIname\*(FC(\*(FIparameter list\*(FC)
1408         {
1409                 \*(FIstatements
1410         \*(FC}\*(FR
1411 .sp .5
1412 .fi
1413 Functions are executed when they are called from within expressions
1414 in either patterns or actions. Actual parameters supplied in the function
1415 call instantiate the formal parameters declared in the function.
1416 Arrays are passed by reference, other variables are passed by value.
1417 .sp .5
1418 Local variables are declared as extra parameters
1419 in the parameter list. The convention is to separate local variables from
1420 real parameters by extra spaces in the parameter list. For example:
1421 .sp .5
1422 .nf
1423         \*(FC# a & b are local
1424         function f(p, q,     a, b)
1425         {
1426                 \&.....
1427         }
1428 .sp .3
1429         /abc/   { ... ; f(1, 2) ; ... }\*(FR
1430 .fi
1431 .sp .5
1432 The left parenthesis in a function call is required
1433 to immediately follow the function name
1434 without any intervening white space.
1435 This is to avoid a syntactic ambiguity with the concatenation operator.
1436 This restriction does not apply to the built-in functions.
1437 .sp .5
1438 Functions may call each other and may be recursive.
1439 Function parameters used as local variables are initialized
1440 to the null string and the number zero upon function invocation.
1441 .sp .5
1442 Use \*(FCreturn\fP to return a value from a function. The return value
1443 is undefined if no value is provided, or if the function returns by
1444 ``falling off'' the end.
1445 .sp .5
1446 \*(CLThe word
1447 \*(FCfunc\fP
1448 may be used in place of
1449 \*(FCfunction\*(FR.
1450 \*(CRNote: This usage is deprecated.\*(CX
1451 .EB "\s+2\f(HBUSER-DEFINED FUNCTIONS\*(FR\s0"
1452
1453
1454
1455 .\" --- Bug Reports
1456 .ES
1457 .fi
1458 \*(CDIf you find a bug in this reference card, please report it via electronic
1459 mail to \*(FCarnold@gnu.org\*(FR.\*(CX
1460 .EB "\s+2\f(HBBUG REPORTS\*(FR\s0"
1461
1462 .BT
1463
1464 .\" --- Environment Variables
1465 .ES
1466 .fi
1467 \*(CDThe environment variable \*(FCAWKPATH\fP specifies a search path to use
1468 when finding source files named with the \*(FC\-f\fP
1469 option.
1470 The default path is
1471 \*(FC".:/usr/local/share/awk"\*(FR,
1472 if this variable does not exist.
1473 (The actual directory may vary,
1474 depending upon how \*(GK was built and installed.)
1475 If a file name given to the \*(FC\-f\fP option contains a ``/'' character,
1476 no path search is performed.
1477 .sp .5
1478 If \*(FCPOSIXLY_CORRECT\fP exists in the environment, then \*(GK
1479 behaves exactly as if \*(FC\-\^\-posix\fP had been specified on the
1480 command line.\*(CB
1481 .EB "\s+2\f(HBENVIRONMENT VARIABLES (\*(GK\f(HB)\*(FR\s0"
1482
1483 .\" --- Historical Features
1484 .ES
1485 .fi
1486 \*(CD\*(GK supports two features of historical AWK implementations.
1487 First, it is possible to call the \*(FClength()\fP
1488 built-in function not only with no argument, but even without parentheses.
1489 This feature is marked as ``deprecated'' in the POSIX standard, and \*(GK
1490 will issue a warning about its use if \*(FC\-\^\-lint\fP
1491 is specified on the command line.
1492 .sp .5
1493 The other feature is the use of \*(FCcontinue\fP
1494 or \*(FCbreak\fP statements outside the body of a
1495 \*(FCwhile\*(FR, \*(FCfor\*(FR, or \*(FCdo\fP loop.
1496 Historical AWK implementations have treated such usage as
1497 equivalent to the \*(FCnext\fP statement.
1498 \*(GK will support this usage if \*(FC\-\^\-traditional\fP
1499 has been specified.\*(CB
1500 .EB "\s+2\f(HBHISTORICAL FEATURES (\*(GK\f(HB)\*(FR\s0"
1501
1502
1503 .\" --- FTP Information
1504 .ES
1505 .nf
1506 \*(CDHost: \*(FCgnudist.gnu.org\*(FR
1507 File: \*(FC/gnu/gawk/gawk-3.0.6.tar.gz\fP
1508 .in +.2i
1509 .fi
1510 GNU \*(AK (\*(GK). There may be a later version.
1511 .in -.2i
1512 .nf
1513 .sp .5
1514 Host: \*(FCnetlib.bell-labs.com\*(FR
1515 File: \*(FC/netlib/research/awk.bundle.gz\fP
1516 .in +.2i
1517 .fi
1518 \*(NK. This version requires an ANSI C compiler;
1519 GCC (the GNU C compiler) works well.
1520 .in -.2i
1521 .nf
1522 .sp .5
1523 Host: \*(FCftp.whidbey.net\*(FR
1524 File: \*(FC/pub/brennan/mawk1.3.3.tar.gz\fP
1525 .in +.2i
1526 .fi
1527 Michael Brennan's \*(MK. There may be a newer version.\*(CX
1528 .in -.2i
1529 .EB "\s+2\f(HBFTP INFORMATION\*(FR\s0"
1530
1531 .\" --- Copying Permissions
1532 .ES
1533 .fi
1534 \*(CDCopyright \(co 1996-2000 Free Software Foundation, Inc.
1535 .sp .5   
1536 Permission is granted to make and distribute verbatim copies of this
1537 reference card provided the copyright notice and this permission notice
1538 are preserved on all copies.
1539 .sp .5
1540 Permission is granted to copy and distribute modified versions of this
1541 reference card under the conditions for verbatim copying, provided that
1542 the entire resulting derived work is distributed under the terms of a
1543 permission notice identical to this one.
1544 .sp .5
1545 Permission is granted to copy and distribute translations of this
1546 reference card into another language, under the above conditions for
1547 modified versions, except that this permission notice may be stated in a
1548 translation approved by the Foundation.\*(CX
1549 .EB "\s+2\f(HBCOPYING PERMISSIONS\*(FR\s0"
1550 .BT