Annotation of src/usr.bin/awk/awk.1, Revision 1.34
1.34 ! jmc 1: .\" $OpenBSD: awk.1,v 1.33 2009/02/08 17:15:09 jmc Exp $
1.7 aaron 2: .\" EX/EE is a Bd
1.11 jmc 3: .\"
4: .\" Copyright (C) Lucent Technologies 1997
5: .\" All Rights Reserved
1.12 jmc 6: .\"
1.11 jmc 7: .\" Permission to use, copy, modify, and distribute this software and
8: .\" its documentation for any purpose and without fee is hereby
9: .\" granted, provided that the above copyright notice appear in all
10: .\" copies and that both that the copyright notice and this
11: .\" permission notice and warranty disclaimer appear in supporting
12: .\" documentation, and that the name Lucent Technologies or any of
13: .\" its entities not be used in advertising or publicity pertaining
14: .\" to distribution of the software without specific, written prior
15: .\" permission.
1.12 jmc 16: .\"
1.11 jmc 17: .\" LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18: .\" INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
19: .\" IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
20: .\" SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21: .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
22: .\" IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
23: .\" ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
24: .\" THIS SOFTWARE.
25: .\"
1.34 ! jmc 26: .Dd $Mdocdate: February 8 2009 $
1.7 aaron 27: .Dt AWK 1
28: .Os
29: .Sh NAME
30: .Nm awk
31: .Nd pattern-directed scanning and processing language
32: .Sh SYNOPSIS
33: .Nm awk
1.16 jmc 34: .Op Fl safe
35: .Op Fl V
36: .Op Fl d Ns Op Ar n
1.7 aaron 37: .Op Fl F Ar fs
1.16 jmc 38: .Oo Fl v Ar var Ns =
39: .Ns Ar value Oc
1.18 jmc 40: .Op Ar prog | Fl f Ar progfile
1.7 aaron 41: .Ar
42: .Nm nawk
43: .Ar ...
44: .Sh DESCRIPTION
45: .Nm
1.1 tholo 46: scans each input
1.7 aaron 47: .Ar file
1.1 tholo 48: for lines that match any of a set of patterns specified literally in
1.7 aaron 49: .Ar prog
1.16 jmc 50: or in one or more files specified as
1.7 aaron 51: .Fl f Ar progfile .
1.16 jmc 52: With each pattern there can be an associated action that will be performed
1.1 tholo 53: when a line of a
1.7 aaron 54: .Ar file
1.1 tholo 55: matches the pattern.
56: Each line is matched against the
57: pattern portion of every pattern-action statement;
58: the associated action is performed for each matched pattern.
1.6 aaron 59: The file name
1.16 jmc 60: .Sq -
1.1 tholo 61: means the standard input.
62: Any
1.7 aaron 63: .Ar file
1.1 tholo 64: of the form
1.16 jmc 65: .Ar var Ns = Ns Ar value
1.1 tholo 66: is treated as an assignment, not a filename,
67: and is executed at the time it would have been opened if it were a filename.
1.16 jmc 68: .Pp
69: The options are as follows:
1.20 jmc 70: .Bl -tag -width "-safe "
1.16 jmc 71: .It Fl d Ns Op Ar n
72: Debug mode.
73: Set debug level to
74: .Ar n ,
75: or 1 if
76: .Ar n
77: is not specified.
78: A value greater than 1 causes
79: .Nm
80: to dump core on fatal errors.
81: .It Fl F Ar fs
82: Define the input field separator to be the regular expression
1.7 aaron 83: .Ar fs .
1.25 jmc 84: .It Fl f Ar progfile
1.16 jmc 85: Read program code from the specified file
1.25 jmc 86: .Ar progfile
1.16 jmc 87: instead of from the command line.
88: .It Fl safe
89: Disable file output
1.17 jmc 90: .Pf ( Ic print No > ,
91: .Ic print No >> ) ,
1.7 aaron 92: process creation
93: .Po
1.17 jmc 94: .Ar cmd | Ic getline ,
95: .Ic print No \&| ,
96: .Ic system
1.7 aaron 97: .Pc
98: and access to the environment
1.17 jmc 99: .Pf ( Va ENVIRON ;
1.18 jmc 100: see the section on variables below).
1.17 jmc 101: This is a first
1.16 jmc 102: .Pq and not very reliable
103: approximation to a
1.7 aaron 104: .Dq safe
105: version of
1.16 jmc 106: .Nm .
107: .It Fl V
108: Print the version number of
109: .Nm
110: to standard output and exit.
111: .It Fl v Ar var Ns = Ns Ar value
112: Assign
113: .Ar value
114: to variable
115: .Ar var
116: before
117: .Ar prog
118: is executed;
119: any number of
120: .Fl v
121: options may be present.
122: .El
1.7 aaron 123: .Pp
1.18 jmc 124: The input is normally made up of input lines
125: .Pq records
126: separated by newlines, or by the value of
127: .Va RS .
128: If
129: .Va RS
130: is null, then any number of blank lines are used as the record separator,
131: and newlines are used as field separators
132: (in addition to the value of
133: .Va FS ) .
134: This is convenient when working with multi-line records.
135: .Pp
1.7 aaron 136: An input line is normally made up of fields separated by whitespace,
1.18 jmc 137: or by the regular expression
1.7 aaron 138: .Va FS .
1.1 tholo 139: The fields are denoted
1.7 aaron 140: .Va $1 , $2 , ... ,
141: while
142: .Va $0
1.1 tholo 143: refers to the entire line.
144: If
1.7 aaron 145: .Va FS
1.1 tholo 146: is null, the input line is split into one field per character.
1.7 aaron 147: .Pp
1.18 jmc 148: Normally, any number of blanks separate fields.
149: In order to set the field separator to a single blank, use the
150: .Fl F
151: option with a value of
152: .Sq [\ \&] .
153: If a field separator of
154: .Sq t
155: is specified,
156: .Nm
157: treats it as if
158: .Sq \et
159: had been specified and uses
160: .Aq TAB
161: as the field separator.
162: In order to use a literal
163: .Sq t
164: as the field separator, use the
165: .Fl F
166: option with a value of
167: .Sq [t] .
168: .Pp
1.1 tholo 169: A pattern-action statement has the form
1.7 aaron 170: .Pp
171: .D1 Ar pattern Ic \&{ Ar action Ic \&}
172: .Pp
1.6 aaron 173: A missing
1.7 aaron 174: .Ic \&{ Ar action Ic \&}
1.1 tholo 175: means print the line;
176: a missing pattern always matches.
177: Pattern-action statements are separated by newlines or semicolons.
1.7 aaron 178: .Pp
1.18 jmc 179: Newlines are permitted after a terminating statement or following a comma
180: .Pq Sq ,\& ,
181: an open brace
182: .Pq Sq { ,
183: a logical AND
184: .Pq Sq && ,
185: a logical OR
186: .Pq Sq || ,
187: after the
188: .Sq do
189: or
190: .Sq else
191: keywords,
192: or after the closing parenthesis of an
193: .Sq if ,
194: .Sq for ,
195: or
196: .Sq while
197: statement.
198: Additionally, a backslash
199: .Pq Sq \e
200: can be used to escape a newline between tokens.
201: .Pp
1.1 tholo 202: An action is a sequence of statements.
203: A statement can be one of the following:
1.7 aaron 204: .Bd -unfilled -offset indent
205: .Ic if ( Xo
206: .Ar expression ) statement \&
207: .Op Ic else Ar statement
208: .Xc
209: .Ic while ( Ar expression ) statement
210: .Ic for ( Xo
211: .Ar expression ; expression ; expression ) statement
212: .Xc
213: .Ic for ( Xo
214: .Ar var Ic in Ar array ) statement
215: .Xc
216: .Ic do Ar statement Ic while ( Ar expression )
217: .Ic break
218: .Ic continue
219: .Ic { Oo Ar statement ... Oc Ic \& }
220: .Ar expression Xo
221: .No "# commonly" \&
222: .Ar var Ic = Ar expression
223: .Xc
224: .Ic print Xo
225: .Op Ar expression-list
1.17 jmc 226: .Op > Ns Ar expression
1.7 aaron 227: .Xc
228: .Ic printf Ar format Xo
229: .Op Ar ... , expression-list
1.17 jmc 230: .Op > Ns Ar expression
1.7 aaron 231: .Xc
232: .Ic return Op Ar expression
233: .Ic next Xo
234: .No "# skip remaining patterns on this input line"
235: .Xc
236: .Ic nextfile Xo
237: .No "# skip rest of this file, open next, start at top"
238: .Xc
239: .Ic delete Ar array Ns Xo
240: .Ic \&[ Ns Ar expression Ns Ic \&]
241: .No \& "# delete an array element"
242: .Xc
243: .Ic delete Ar array Xo
244: .No "# delete all elements of array"
245: .Xc
246: .Ic exit Xo
247: .Op Ar expression
248: .No \& "# exit immediately; status is" Ar expression
249: .Xc
250: .Ed
251: .Pp
1.1 tholo 252: Statements are terminated by
253: semicolons, newlines or right braces.
254: An empty
1.7 aaron 255: .Ar expression-list
1.1 tholo 256: stands for
1.7 aaron 257: .Ar $0 .
258: String constants are quoted
259: .Li \&"" ,
1.20 jmc 260: with the usual C escapes recognized within
261: (see
262: .Xr printf 1
263: for a complete list of these).
1.1 tholo 264: Expressions take on string or numeric values as appropriate,
265: and are built using the operators
1.7 aaron 266: .Ic + \- * / % ^
1.20 jmc 267: .Pq exponentiation ,
268: and concatenation
269: .Pq indicated by whitespace .
1.1 tholo 270: The operators
1.16 jmc 271: .Ic \&! ++ \-\- += \-= *= /= %= ^=
272: .Ic > >= < <= == != ?:
1.1 tholo 273: are also available in expressions.
274: Variables may be scalars, array elements
275: (denoted
1.7 aaron 276: .Li x[i] )
1.1 tholo 277: or fields.
278: Variables are initialized to the null string.
279: Array subscripts may be any string,
280: not necessarily numeric;
281: this allows for a form of associative memory.
282: Multiple subscripts such as
1.7 aaron 283: .Li [i,j,k]
1.1 tholo 284: are permitted; the constituents are concatenated,
285: separated by the value of
1.17 jmc 286: .Va SUBSEP
1.31 deraadt 287: .Pq see the section on variables below .
1.7 aaron 288: .Pp
1.1 tholo 289: The
1.7 aaron 290: .Ic print
1.1 tholo 291: statement prints its arguments on the standard output
292: (or on a file if
1.17 jmc 293: .Pf > Ns Ar file
1.1 tholo 294: or
1.17 jmc 295: .Pf >> Ns Ar file
1.1 tholo 296: is present or on a pipe if
1.17 jmc 297: .Pf |\ \& Ar cmd
1.1 tholo 298: is present), separated by the current output field separator,
299: and terminated by the output record separator.
1.7 aaron 300: .Ar file
1.1 tholo 301: and
1.7 aaron 302: .Ar cmd
1.1 tholo 303: may be literal names or parenthesized expressions;
304: identical string values in different statements denote
305: the same open file.
306: The
1.7 aaron 307: .Ic printf
1.1 tholo 308: statement formats its expression list according to the format
309: (see
1.28 jmc 310: .Xr printf 1 ) .
1.18 jmc 311: .Pp
312: Patterns are arbitrary Boolean combinations
313: (with
314: .Ic "\&! || &&" )
315: of regular expressions and
316: relational expressions.
1.22 jmc 317: .Nm
318: supports extended regular expressions
319: .Pq EREs .
320: See
321: .Xr re_format 7
322: for more information on regular expressions.
1.18 jmc 323: Isolated regular expressions
324: in a pattern apply to the entire line.
325: Regular expressions may also occur in
326: relational expressions, using the operators
327: .Ic ~
328: and
329: .Ic !~ .
330: .Pf / Ns Ar re Ns /
331: is a constant regular expression;
332: any string (constant or variable) may be used
333: as a regular expression, except in the position of an isolated regular expression
334: in a pattern.
335: .Pp
336: A pattern may consist of two patterns separated by a comma;
337: in this case, the action is performed for all lines
338: from an occurrence of the first pattern
339: through an occurrence of the second.
340: .Pp
341: A relational expression is one of the following:
342: .Bd -unfilled -offset indent
343: .Ar expression matchop regular-expression
344: .Ar expression relop expression
345: .Ar expression Ic in Ar array-name
346: .Ic \&( Ns Xo
347: .Ar expr , expr , \&... Ns Ic \&) in
348: .Ar \& array-name
349: .Xc
350: .Ed
351: .Pp
352: where a
353: .Ar relop
354: is any of the six relational operators in C, and a
355: .Ar matchop
356: is either
357: .Ic ~
358: (matches)
359: or
360: .Ic !~
361: (does not match).
362: A conditional is an arithmetic expression,
363: a relational expression,
364: or a Boolean combination
365: of these.
366: .Pp
367: The special patterns
368: .Ic BEGIN
369: and
370: .Ic END
371: may be used to capture control before the first input line is read
372: and after the last.
373: .Ic BEGIN
374: and
375: .Ic END
376: do not combine with other patterns.
377: .Pp
378: Variable names with special meanings:
379: .Pp
1.20 jmc 380: .Bl -tag -width "FILENAME " -compact
1.18 jmc 381: .It Va ARGC
382: Argument count, assignable.
383: .It Va ARGV
384: Argument array, assignable;
385: non-null members are taken as filenames.
386: .It Va CONVFMT
387: Conversion format when converting numbers
388: (default
389: .Qq Li %.6g ) .
390: .It Va ENVIRON
391: Array of environment variables; subscripts are names.
392: .It Va FILENAME
393: The name of the current input file.
394: .It Va FNR
395: Ordinal number of the current record in the current file.
396: .It Va FS
397: Regular expression used to separate fields; also settable
398: by option
399: .Fl F Ar fs .
400: .It Va NF
401: Number of fields in the current record.
402: .Va $NF
403: can be used to obtain the value of the last field in the current record.
404: .It Va NR
405: Ordinal number of the current record.
406: .It Va OFMT
407: Output format for numbers (default
408: .Qq Li %.6g ) .
409: .It Va OFS
410: Output field separator (default blank).
411: .It Va ORS
412: Output record separator (default newline).
413: .It Va RLENGTH
414: The length of the string matched by the
415: .Fn match
416: function.
417: .It Va RS
418: Input record separator (default newline).
419: .It Va RSTART
420: The starting position of the string matched by the
421: .Fn match
422: function.
423: .It Va SUBSEP
424: Separates multiple subscripts (default 034).
425: .El
1.17 jmc 426: .Sh FUNCTIONS
427: The awk language has a variety of built-in functions:
1.30 jmc 428: arithmetic, string, input/output, general, and bit-operation.
429: .Pp
430: Functions may be defined (at the position of a pattern-action statement)
431: thusly:
432: .Pp
433: .Dl function foo(a, b, c) { ...; return x }
434: .Pp
435: Parameters are passed by value if scalar, and by reference if array name;
436: functions may be called recursively.
437: Parameters are local to the function; all other variables are global.
438: Thus local variables may be created by providing excess parameters in
439: the function definition.
1.17 jmc 440: .Ss Arithmetic Functions
441: .Bl -tag -width "atan2(y, x)"
442: .It Fn atan2 y x
443: Return the arctangent of
444: .Fa y Ns / Ns Fa x
445: in radians.
446: .It Fn cos x
447: Return the cosine of
448: .Fa x ,
449: where
450: .Fa x
451: is in radians.
452: .It Fn exp x
453: Return the exponential of
454: .Fa x .
455: .It Fn int x
456: Return
457: .Fa x
458: truncated to an integer value.
459: .It Fn log x
460: Return the natural logarithm of
461: .Fa x .
1.7 aaron 462: .It Fn rand
1.17 jmc 463: Return a random number,
464: .Fa n ,
465: such that
466: .Sm off
467: .Pf 0 \*(Le Fa n No \*(Lt 1 .
468: .Sm on
469: .It Fn sin x
470: Return the sine of
471: .Fa x ,
472: where
473: .Fa x
474: is in radians.
475: .It Fn sqrt x
476: Return the square root of
477: .Fa x .
478: .It Fn srand expr
1.16 jmc 479: Sets seed for
1.7 aaron 480: .Fn rand
1.17 jmc 481: to
482: .Fa expr
1.1 tholo 483: and returns the previous seed.
1.17 jmc 484: If
485: .Fa expr
486: is omitted, the time of day is used instead.
487: .El
488: .Ss String Functions
489: .Bl -tag -width "split(s, a, fs)"
490: .It Fn gsub r t s
491: The same as
492: .Fn sub
493: except that all occurrences of the regular expression are replaced.
494: .Fn gsub
495: returns the number of replacements.
1.7 aaron 496: .It Fn index s t
1.16 jmc 497: The position in
1.7 aaron 498: .Fa s
1.1 tholo 499: where the string
1.7 aaron 500: .Fa t
1.1 tholo 501: occurs, or 0 if it does not.
1.17 jmc 502: .It Fn length s
503: The length of
504: .Fa s
505: taken as a string,
506: or of
507: .Va $0
508: if no argument is given.
1.7 aaron 509: .It Fn match s r
1.16 jmc 510: The position in
1.7 aaron 511: .Fa s
1.1 tholo 512: where the regular expression
1.7 aaron 513: .Fa r
1.1 tholo 514: occurs, or 0 if it does not.
1.17 jmc 515: The variable
1.7 aaron 516: .Va RSTART
1.17 jmc 517: is set to the starting position of the matched string
518: .Pq which is the same as the returned value
519: or zero if no match is found.
520: The variable
1.7 aaron 521: .Va RLENGTH
1.17 jmc 522: is set to the length of the matched string,
523: or \-1 if no match is found.
1.7 aaron 524: .It Fn split s a fs
1.16 jmc 525: Splits the string
1.7 aaron 526: .Fa s
1.1 tholo 527: into array elements
1.7 aaron 528: .Va a[1] , a[2] , ... , a[n]
1.1 tholo 529: and returns
1.7 aaron 530: .Va n .
1.1 tholo 531: The separation is done with the regular expression
1.7 aaron 532: .Ar fs
1.1 tholo 533: or with the field separator
1.7 aaron 534: .Va FS
1.1 tholo 535: if
1.7 aaron 536: .Ar fs
1.1 tholo 537: is not given.
538: An empty string as field separator splits the string
539: into one array element per character.
1.17 jmc 540: .It Fn sprintf fmt expr ...
541: The string resulting from formatting
542: .Fa expr , ...
543: according to the
1.28 jmc 544: .Xr printf 1
1.17 jmc 545: format
546: .Fa fmt .
1.7 aaron 547: .It Fn sub r t s
1.16 jmc 548: Substitutes
1.7 aaron 549: .Fa t
1.1 tholo 550: for the first occurrence of the regular expression
1.7 aaron 551: .Fa r
1.1 tholo 552: in the string
1.7 aaron 553: .Fa s .
1.1 tholo 554: If
1.7 aaron 555: .Fa s
1.1 tholo 556: is not given,
1.7 aaron 557: .Va $0
1.1 tholo 558: is used.
1.17 jmc 559: An ampersand
560: .Pq Sq &
561: in
562: .Fa t
563: is replaced in string
564: .Fa s
565: with regular expression
566: .Fa r .
567: A literal ampersand can be specified by preceding it with two backslashes
568: .Pq Sq \e\e .
569: A literal backslash can be specified by preceding it with another backslash
570: .Pq Sq \e\e .
1.7 aaron 571: .Fn sub
1.17 jmc 572: returns the number of replacements.
573: .It Fn substr s m n
574: Return at most the
575: .Fa n Ns -character
576: substring of
577: .Fa s
578: that begins at position
579: .Fa m
580: counted from 1.
581: If
582: .Fa n
583: is omitted, or if
584: .Fa n
585: specifies more characters than are left in the string,
586: the length of the substring is limited by the length of
587: .Fa s .
1.7 aaron 588: .It Fn tolower str
1.16 jmc 589: Returns a copy of
1.7 aaron 590: .Fa str
1.1 tholo 591: with all upper-case characters translated to their
592: corresponding lower-case equivalents.
1.7 aaron 593: .It Fn toupper str
1.16 jmc 594: Returns a copy of
1.7 aaron 595: .Fa str
1.1 tholo 596: with all lower-case characters translated to their
597: corresponding upper-case equivalents.
1.7 aaron 598: .El
1.17 jmc 599: .Ss Input/Output and General Functions
600: .Bl -tag -width "getline [var] < file"
601: .It Fn close expr
602: Closes the file or pipe
603: .Fa expr .
604: .Fa expr
605: should match the string that was used to open the file or pipe.
606: .It Ar cmd | Ic getline Op Va var
607: Read a record of input from a stream piped from the output of
608: .Ar cmd .
609: If
610: .Va var
611: is omitted, the variables
612: .Va $0
613: and
614: .Va NF
615: are set.
616: Otherwise
617: .Va var
618: is set.
619: If the stream is not open, it is opened.
620: As long as the stream remains open, subsequent calls
621: will read subsequent records from the stream.
622: The stream remains open until explicitly closed with a call to
623: .Fn close .
1.24 jmc 624: .Ic getline
625: returns 1 for a successful input, 0 for end of file, and \-1 for an error.
626: .It Fn fflush [expr]
627: Flushes any buffered output for the file, pipe
628: .Fa expr ,
629: or all open files or pipes if
630: .Fa expr
631: is omitted.
1.17 jmc 632: .Fa expr
633: should match the string that was used to open the file or pipe.
634: .It Ic getline
635: Sets
636: .Va $0
637: to the next input record from the current input file.
638: This form of
639: .Ic getline
640: sets the variables
641: .Va NF ,
642: .Va NR ,
643: and
644: .Va FNR .
1.7 aaron 645: .Ic getline
1.17 jmc 646: returns 1 for a successful input, 0 for end of file, and \-1 for an error.
647: .It Ic getline Va var
648: Sets
1.7 aaron 649: .Va $0
1.17 jmc 650: to variable
651: .Va var .
652: This form of
653: .Ic getline
654: sets the variables
655: .Va NR
656: and
657: .Va FNR .
658: .Ic getline
659: returns 1 for a successful input, 0 for end of file, and \-1 for an error.
660: .It Xo
661: .Ic getline Op Va var
662: .Pf \ \&< Ar file
663: .Xc
664: Sets
1.7 aaron 665: .Va $0
1.1 tholo 666: to the next record from
1.7 aaron 667: .Ar file .
1.17 jmc 668: If
669: .Va var
670: is omitted, the variables
671: .Va $0
672: and
673: .Va NF
674: are set.
675: Otherwise
676: .Va var
677: is set.
678: If
679: .Ar file
680: is not open, it is opened.
681: As long as the stream remains open, subsequent calls will read subsequent
682: records from
683: .Ar file .
684: .Ar file
685: remains open until explicitly closed with a call to
686: .Fn close .
687: .It Fn system cmd
688: Executes
689: .Fa cmd
690: and returns its exit status.
691: .El
1.30 jmc 692: .Ss Bit-Operation Functions
1.29 pyr 693: .Bl -tag -width "lshift(a, b)"
694: .It Fn compl x
695: Returns the bitwise complement of integer argument x.
696: .It Fn and x y
1.30 jmc 697: Performs a bitwise AND on integer arguments x and y.
1.29 pyr 698: .It Fn or x y
1.30 jmc 699: Performs a bitwise OR on integer arguments x and y.
1.29 pyr 700: .It Fn xor x y
1.30 jmc 701: Performs a bitwise Exclusive-OR on integer arguments x and y.
1.29 pyr 702: .It Fn lshift x n
703: Returns x shifted by n bits to the left.
704: .It Fn rshift x n
705: Returns y shifted by n bits to the right.
706: .El
1.7 aaron 707: .Sh EXAMPLES
1.16 jmc 708: Print lines longer than 72 characters:
709: .Pp
1.7 aaron 710: .Dl length($0) > 72
1.16 jmc 711: .Pp
712: Print first two fields in opposite order:
1.7 aaron 713: .Pp
714: .Dl { print $2, $1 }
1.16 jmc 715: .Pp
716: Same, with input fields separated by comma and/or blanks and tabs:
1.7 aaron 717: .Bd -literal -offset indent
1.1 tholo 718: BEGIN { FS = ",[ \et]*|[ \et]+" }
719: { print $2, $1 }
1.7 aaron 720: .Ed
1.16 jmc 721: .Pp
722: Add up first column, print sum and average:
1.7 aaron 723: .Bd -literal -offset indent
724: { s += $1 }
725: END { print "sum is", s, " average is", s/NR }
726: .Ed
1.16 jmc 727: .Pp
728: Print all lines between start/stop pairs:
1.7 aaron 729: .Pp
730: .Dl /start/, /stop/
1.16 jmc 731: .Pp
732: Simulate echo(1):
1.7 aaron 733: .Bd -literal -offset indent
734: BEGIN { # Simulate echo(1)
735: for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
736: printf "\en"
737: exit }
1.19 jmc 738: .Ed
739: .Pp
740: Print an error message to standard error:
741: .Bd -literal -offset indent
742: { print "error!" > "/dev/stderr" }
1.7 aaron 743: .Ed
744: .Sh SEE ALSO
745: .Xr lex 1 ,
1.20 jmc 746: .Xr printf 1 ,
1.16 jmc 747: .Xr sed 1 ,
1.23 jmc 748: .Xr re_format 7 ,
749: .Xr script 7
1.7 aaron 750: .Rs
751: .%A A. V. Aho
752: .%A B. W. Kernighan
753: .%A P. J. Weinberger
754: .%T The AWK Programming Language
755: .%I Addison-Wesley
756: .%D 1988
757: .%O ISBN 0-201-07981-X
758: .Re
1.26 jmc 759: .Sh STANDARDS
760: The
761: .Nm
762: utility is compliant with the
1.33 jmc 763: .St -p1003.1-2008
1.26 jmc 764: specification.
765: .Pp
766: The flags
767: .Op Fl \&dV
768: and
769: .Op Fl safe ,
1.30 jmc 770: as well as the commands
771: .Cm fflush , compl , and , or ,
772: .Cm xor , lshift , rshift ,
1.26 jmc 773: are extensions to that specification.
1.32 jmc 774: .Pp
775: .Nm
776: does not support {n,m} pattern matching.
1.8 aaron 777: .Sh HISTORY
1.13 millert 778: An
1.8 aaron 779: .Nm
1.13 millert 780: utility appeared in
781: .At v7 .
1.7 aaron 782: .Sh BUGS
1.1 tholo 783: There are no explicit conversions between numbers and strings.
784: To force an expression to be treated as a number add 0 to it;
785: to force it to be treated as a string concatenate
1.7 aaron 786: .Li \&""
787: to it.
788: .Pp
1.1 tholo 789: The scope rules for variables in functions are a botch;
790: the syntax is worse.