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