Annotation of src/usr.bin/awk/awk.1, Revision 1.35
1.35 ! jmc 1: .\" $OpenBSD: awk.1,v 1.34 2010/01/10 10:53:33 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.35 ! jmc 26: .Dd $Mdocdate: January 10 2010 $
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.35 ! jmc 204: .Pp
! 205: .Bl -tag -width Ds -offset indent -compact
! 206: .It Xo Ic if ( Ar expression ) Ar statement
1.7 aaron 207: .Op Ic else Ar statement
208: .Xc
1.35 ! jmc 209: .It Ic while ( Ar expression ) Ar statement
! 210: .It Xo Ic for
! 211: .No ( Ar expression ; expression ; expression ) statement
! 212: .Xc
! 213: .It Xo Ic for
! 214: .No ( Ar var Ic in Ar array ) statement
! 215: .Xc
! 216: .It Xo Ic do
! 217: .Ar statement Ic while ( Ar expression )
! 218: .Xc
! 219: .It Ic break
! 220: .It Ic continue
! 221: .It Xo Ic {
! 222: .Op Ar statement ...
! 223: .Ic }
! 224: .Xc
! 225: .It Xo Ar expression
! 226: .No # commonly
! 227: .Ar var No = Ar expression
1.7 aaron 228: .Xc
1.35 ! jmc 229: .It Xo Ic print
1.7 aaron 230: .Op Ar expression-list
1.17 jmc 231: .Op > Ns Ar expression
1.7 aaron 232: .Xc
1.35 ! jmc 233: .It Xo Ic printf Ar format
1.7 aaron 234: .Op Ar ... , expression-list
1.17 jmc 235: .Op > Ns Ar expression
1.7 aaron 236: .Xc
1.35 ! jmc 237: .It Ic return Op Ar expression
! 238: .It Xo Ic next
! 239: .No # skip remaining patterns on this input line
! 240: .Xc
! 241: .It Xo Ic nextfile
! 242: .No # skip rest of this file, open next, start at top
! 243: .Xc
! 244: .It Xo Ic delete
! 245: .Sm off
! 246: .Ar array Ic \&[ Ar expression Ic \&]
! 247: .Sm on
! 248: .No # delete an array element
1.7 aaron 249: .Xc
1.35 ! jmc 250: .It Xo Ic delete Ar array
! 251: .No # delete all elements of array
1.7 aaron 252: .Xc
1.35 ! jmc 253: .It Xo Ic exit
1.7 aaron 254: .Op Ar expression
1.35 ! jmc 255: .No # exit immediately; status is Ar expression
1.7 aaron 256: .Xc
1.35 ! jmc 257: .El
1.7 aaron 258: .Pp
1.1 tholo 259: Statements are terminated by
260: semicolons, newlines or right braces.
261: An empty
1.7 aaron 262: .Ar expression-list
1.1 tholo 263: stands for
1.7 aaron 264: .Ar $0 .
265: String constants are quoted
266: .Li \&"" ,
1.20 jmc 267: with the usual C escapes recognized within
268: (see
269: .Xr printf 1
270: for a complete list of these).
1.1 tholo 271: Expressions take on string or numeric values as appropriate,
272: and are built using the operators
1.7 aaron 273: .Ic + \- * / % ^
1.20 jmc 274: .Pq exponentiation ,
275: and concatenation
276: .Pq indicated by whitespace .
1.1 tholo 277: The operators
1.16 jmc 278: .Ic \&! ++ \-\- += \-= *= /= %= ^=
279: .Ic > >= < <= == != ?:
1.1 tholo 280: are also available in expressions.
281: Variables may be scalars, array elements
282: (denoted
1.7 aaron 283: .Li x[i] )
1.1 tholo 284: or fields.
285: Variables are initialized to the null string.
286: Array subscripts may be any string,
287: not necessarily numeric;
288: this allows for a form of associative memory.
289: Multiple subscripts such as
1.7 aaron 290: .Li [i,j,k]
1.1 tholo 291: are permitted; the constituents are concatenated,
292: separated by the value of
1.17 jmc 293: .Va SUBSEP
1.31 deraadt 294: .Pq see the section on variables below .
1.7 aaron 295: .Pp
1.1 tholo 296: The
1.7 aaron 297: .Ic print
1.1 tholo 298: statement prints its arguments on the standard output
299: (or on a file if
1.17 jmc 300: .Pf > Ns Ar file
1.1 tholo 301: or
1.17 jmc 302: .Pf >> Ns Ar file
1.1 tholo 303: is present or on a pipe if
1.17 jmc 304: .Pf |\ \& Ar cmd
1.1 tholo 305: is present), separated by the current output field separator,
306: and terminated by the output record separator.
1.7 aaron 307: .Ar file
1.1 tholo 308: and
1.7 aaron 309: .Ar cmd
1.1 tholo 310: may be literal names or parenthesized expressions;
311: identical string values in different statements denote
312: the same open file.
313: The
1.7 aaron 314: .Ic printf
1.1 tholo 315: statement formats its expression list according to the format
316: (see
1.28 jmc 317: .Xr printf 1 ) .
1.18 jmc 318: .Pp
319: Patterns are arbitrary Boolean combinations
320: (with
321: .Ic "\&! || &&" )
322: of regular expressions and
323: relational expressions.
1.22 jmc 324: .Nm
325: supports extended regular expressions
326: .Pq EREs .
327: See
328: .Xr re_format 7
329: for more information on regular expressions.
1.18 jmc 330: Isolated regular expressions
331: in a pattern apply to the entire line.
332: Regular expressions may also occur in
333: relational expressions, using the operators
334: .Ic ~
335: and
336: .Ic !~ .
337: .Pf / Ns Ar re Ns /
338: is a constant regular expression;
339: any string (constant or variable) may be used
340: as a regular expression, except in the position of an isolated regular expression
341: in a pattern.
342: .Pp
343: A pattern may consist of two patterns separated by a comma;
344: in this case, the action is performed for all lines
345: from an occurrence of the first pattern
346: through an occurrence of the second.
347: .Pp
348: A relational expression is one of the following:
1.35 ! jmc 349: .Pp
! 350: .Bl -tag -width Ds -offset indent -compact
! 351: .It Ar expression matchop regular-expression
! 352: .It Ar expression relop expression
! 353: .It Ar expression Ic in Ar array-name
! 354: .It Xo Ic \&( Ns
1.18 jmc 355: .Ar expr , expr , \&... Ns Ic \&) in
1.35 ! jmc 356: .Ar array-name
1.18 jmc 357: .Xc
1.35 ! jmc 358: .El
1.18 jmc 359: .Pp
360: where a
361: .Ar relop
362: is any of the six relational operators in C, and a
363: .Ar matchop
364: is either
365: .Ic ~
366: (matches)
367: or
368: .Ic !~
369: (does not match).
370: A conditional is an arithmetic expression,
371: a relational expression,
372: or a Boolean combination
373: of these.
374: .Pp
375: The special patterns
376: .Ic BEGIN
377: and
378: .Ic END
379: may be used to capture control before the first input line is read
380: and after the last.
381: .Ic BEGIN
382: and
383: .Ic END
384: do not combine with other patterns.
385: .Pp
386: Variable names with special meanings:
387: .Pp
1.20 jmc 388: .Bl -tag -width "FILENAME " -compact
1.18 jmc 389: .It Va ARGC
390: Argument count, assignable.
391: .It Va ARGV
392: Argument array, assignable;
393: non-null members are taken as filenames.
394: .It Va CONVFMT
395: Conversion format when converting numbers
396: (default
397: .Qq Li %.6g ) .
398: .It Va ENVIRON
399: Array of environment variables; subscripts are names.
400: .It Va FILENAME
401: The name of the current input file.
402: .It Va FNR
403: Ordinal number of the current record in the current file.
404: .It Va FS
405: Regular expression used to separate fields; also settable
406: by option
407: .Fl F Ar fs .
408: .It Va NF
409: Number of fields in the current record.
410: .Va $NF
411: can be used to obtain the value of the last field in the current record.
412: .It Va NR
413: Ordinal number of the current record.
414: .It Va OFMT
415: Output format for numbers (default
416: .Qq Li %.6g ) .
417: .It Va OFS
418: Output field separator (default blank).
419: .It Va ORS
420: Output record separator (default newline).
421: .It Va RLENGTH
422: The length of the string matched by the
423: .Fn match
424: function.
425: .It Va RS
426: Input record separator (default newline).
427: .It Va RSTART
428: The starting position of the string matched by the
429: .Fn match
430: function.
431: .It Va SUBSEP
432: Separates multiple subscripts (default 034).
433: .El
1.17 jmc 434: .Sh FUNCTIONS
435: The awk language has a variety of built-in functions:
1.30 jmc 436: arithmetic, string, input/output, general, and bit-operation.
437: .Pp
438: Functions may be defined (at the position of a pattern-action statement)
439: thusly:
440: .Pp
441: .Dl function foo(a, b, c) { ...; return x }
442: .Pp
443: Parameters are passed by value if scalar, and by reference if array name;
444: functions may be called recursively.
445: Parameters are local to the function; all other variables are global.
446: Thus local variables may be created by providing excess parameters in
447: the function definition.
1.17 jmc 448: .Ss Arithmetic Functions
449: .Bl -tag -width "atan2(y, x)"
450: .It Fn atan2 y x
451: Return the arctangent of
452: .Fa y Ns / Ns Fa x
453: in radians.
454: .It Fn cos x
455: Return the cosine of
456: .Fa x ,
457: where
458: .Fa x
459: is in radians.
460: .It Fn exp x
461: Return the exponential of
462: .Fa x .
463: .It Fn int x
464: Return
465: .Fa x
466: truncated to an integer value.
467: .It Fn log x
468: Return the natural logarithm of
469: .Fa x .
1.7 aaron 470: .It Fn rand
1.17 jmc 471: Return a random number,
472: .Fa n ,
473: such that
474: .Sm off
475: .Pf 0 \*(Le Fa n No \*(Lt 1 .
476: .Sm on
477: .It Fn sin x
478: Return the sine of
479: .Fa x ,
480: where
481: .Fa x
482: is in radians.
483: .It Fn sqrt x
484: Return the square root of
485: .Fa x .
486: .It Fn srand expr
1.16 jmc 487: Sets seed for
1.7 aaron 488: .Fn rand
1.17 jmc 489: to
490: .Fa expr
1.1 tholo 491: and returns the previous seed.
1.17 jmc 492: If
493: .Fa expr
494: is omitted, the time of day is used instead.
495: .El
496: .Ss String Functions
497: .Bl -tag -width "split(s, a, fs)"
498: .It Fn gsub r t s
499: The same as
500: .Fn sub
501: except that all occurrences of the regular expression are replaced.
502: .Fn gsub
503: returns the number of replacements.
1.7 aaron 504: .It Fn index s t
1.16 jmc 505: The position in
1.7 aaron 506: .Fa s
1.1 tholo 507: where the string
1.7 aaron 508: .Fa t
1.1 tholo 509: occurs, or 0 if it does not.
1.17 jmc 510: .It Fn length s
511: The length of
512: .Fa s
513: taken as a string,
514: or of
515: .Va $0
516: if no argument is given.
1.7 aaron 517: .It Fn match s r
1.16 jmc 518: The position in
1.7 aaron 519: .Fa s
1.1 tholo 520: where the regular expression
1.7 aaron 521: .Fa r
1.1 tholo 522: occurs, or 0 if it does not.
1.17 jmc 523: The variable
1.7 aaron 524: .Va RSTART
1.17 jmc 525: is set to the starting position of the matched string
526: .Pq which is the same as the returned value
527: or zero if no match is found.
528: The variable
1.7 aaron 529: .Va RLENGTH
1.17 jmc 530: is set to the length of the matched string,
531: or \-1 if no match is found.
1.7 aaron 532: .It Fn split s a fs
1.16 jmc 533: Splits the string
1.7 aaron 534: .Fa s
1.1 tholo 535: into array elements
1.7 aaron 536: .Va a[1] , a[2] , ... , a[n]
1.1 tholo 537: and returns
1.7 aaron 538: .Va n .
1.1 tholo 539: The separation is done with the regular expression
1.7 aaron 540: .Ar fs
1.1 tholo 541: or with the field separator
1.7 aaron 542: .Va FS
1.1 tholo 543: if
1.7 aaron 544: .Ar fs
1.1 tholo 545: is not given.
546: An empty string as field separator splits the string
547: into one array element per character.
1.17 jmc 548: .It Fn sprintf fmt expr ...
549: The string resulting from formatting
550: .Fa expr , ...
551: according to the
1.28 jmc 552: .Xr printf 1
1.17 jmc 553: format
554: .Fa fmt .
1.7 aaron 555: .It Fn sub r t s
1.16 jmc 556: Substitutes
1.7 aaron 557: .Fa t
1.1 tholo 558: for the first occurrence of the regular expression
1.7 aaron 559: .Fa r
1.1 tholo 560: in the string
1.7 aaron 561: .Fa s .
1.1 tholo 562: If
1.7 aaron 563: .Fa s
1.1 tholo 564: is not given,
1.7 aaron 565: .Va $0
1.1 tholo 566: is used.
1.17 jmc 567: An ampersand
568: .Pq Sq &
569: in
570: .Fa t
571: is replaced in string
572: .Fa s
573: with regular expression
574: .Fa r .
575: A literal ampersand can be specified by preceding it with two backslashes
576: .Pq Sq \e\e .
577: A literal backslash can be specified by preceding it with another backslash
578: .Pq Sq \e\e .
1.7 aaron 579: .Fn sub
1.17 jmc 580: returns the number of replacements.
581: .It Fn substr s m n
582: Return at most the
583: .Fa n Ns -character
584: substring of
585: .Fa s
586: that begins at position
587: .Fa m
588: counted from 1.
589: If
590: .Fa n
591: is omitted, or if
592: .Fa n
593: specifies more characters than are left in the string,
594: the length of the substring is limited by the length of
595: .Fa s .
1.7 aaron 596: .It Fn tolower str
1.16 jmc 597: Returns a copy of
1.7 aaron 598: .Fa str
1.1 tholo 599: with all upper-case characters translated to their
600: corresponding lower-case equivalents.
1.7 aaron 601: .It Fn toupper str
1.16 jmc 602: Returns a copy of
1.7 aaron 603: .Fa str
1.1 tholo 604: with all lower-case characters translated to their
605: corresponding upper-case equivalents.
1.7 aaron 606: .El
1.17 jmc 607: .Ss Input/Output and General Functions
608: .Bl -tag -width "getline [var] < file"
609: .It Fn close expr
610: Closes the file or pipe
611: .Fa expr .
612: .Fa expr
613: should match the string that was used to open the file or pipe.
614: .It Ar cmd | Ic getline Op Va var
615: Read a record of input from a stream piped from the output of
616: .Ar cmd .
617: If
618: .Va var
619: is omitted, the variables
620: .Va $0
621: and
622: .Va NF
623: are set.
624: Otherwise
625: .Va var
626: is set.
627: If the stream is not open, it is opened.
628: As long as the stream remains open, subsequent calls
629: will read subsequent records from the stream.
630: The stream remains open until explicitly closed with a call to
631: .Fn close .
1.24 jmc 632: .Ic getline
633: returns 1 for a successful input, 0 for end of file, and \-1 for an error.
634: .It Fn fflush [expr]
635: Flushes any buffered output for the file, pipe
636: .Fa expr ,
637: or all open files or pipes if
638: .Fa expr
639: is omitted.
1.17 jmc 640: .Fa expr
641: should match the string that was used to open the file or pipe.
642: .It Ic getline
643: Sets
644: .Va $0
645: to the next input record from the current input file.
646: This form of
647: .Ic getline
648: sets the variables
649: .Va NF ,
650: .Va NR ,
651: and
652: .Va FNR .
1.7 aaron 653: .Ic getline
1.17 jmc 654: returns 1 for a successful input, 0 for end of file, and \-1 for an error.
655: .It Ic getline Va var
656: Sets
1.7 aaron 657: .Va $0
1.17 jmc 658: to variable
659: .Va var .
660: This form of
661: .Ic getline
662: sets the variables
663: .Va NR
664: and
665: .Va FNR .
666: .Ic getline
667: returns 1 for a successful input, 0 for end of file, and \-1 for an error.
668: .It Xo
669: .Ic getline Op Va var
670: .Pf \ \&< Ar file
671: .Xc
672: Sets
1.7 aaron 673: .Va $0
1.1 tholo 674: to the next record from
1.7 aaron 675: .Ar file .
1.17 jmc 676: If
677: .Va var
678: is omitted, the variables
679: .Va $0
680: and
681: .Va NF
682: are set.
683: Otherwise
684: .Va var
685: is set.
686: If
687: .Ar file
688: is not open, it is opened.
689: As long as the stream remains open, subsequent calls will read subsequent
690: records from
691: .Ar file .
692: .Ar file
693: remains open until explicitly closed with a call to
694: .Fn close .
695: .It Fn system cmd
696: Executes
697: .Fa cmd
698: and returns its exit status.
699: .El
1.30 jmc 700: .Ss Bit-Operation Functions
1.29 pyr 701: .Bl -tag -width "lshift(a, b)"
702: .It Fn compl x
703: Returns the bitwise complement of integer argument x.
704: .It Fn and x y
1.30 jmc 705: Performs a bitwise AND on integer arguments x and y.
1.29 pyr 706: .It Fn or x y
1.30 jmc 707: Performs a bitwise OR on integer arguments x and y.
1.29 pyr 708: .It Fn xor x y
1.30 jmc 709: Performs a bitwise Exclusive-OR on integer arguments x and y.
1.29 pyr 710: .It Fn lshift x n
711: Returns x shifted by n bits to the left.
712: .It Fn rshift x n
713: Returns y shifted by n bits to the right.
714: .El
1.7 aaron 715: .Sh EXAMPLES
1.16 jmc 716: Print lines longer than 72 characters:
717: .Pp
1.7 aaron 718: .Dl length($0) > 72
1.16 jmc 719: .Pp
720: Print first two fields in opposite order:
1.7 aaron 721: .Pp
722: .Dl { print $2, $1 }
1.16 jmc 723: .Pp
724: Same, with input fields separated by comma and/or blanks and tabs:
1.7 aaron 725: .Bd -literal -offset indent
1.1 tholo 726: BEGIN { FS = ",[ \et]*|[ \et]+" }
727: { print $2, $1 }
1.7 aaron 728: .Ed
1.16 jmc 729: .Pp
730: Add up first column, print sum and average:
1.7 aaron 731: .Bd -literal -offset indent
732: { s += $1 }
733: END { print "sum is", s, " average is", s/NR }
734: .Ed
1.16 jmc 735: .Pp
736: Print all lines between start/stop pairs:
1.7 aaron 737: .Pp
738: .Dl /start/, /stop/
1.16 jmc 739: .Pp
740: Simulate echo(1):
1.7 aaron 741: .Bd -literal -offset indent
742: BEGIN { # Simulate echo(1)
743: for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
744: printf "\en"
745: exit }
1.19 jmc 746: .Ed
747: .Pp
748: Print an error message to standard error:
749: .Bd -literal -offset indent
750: { print "error!" > "/dev/stderr" }
1.7 aaron 751: .Ed
752: .Sh SEE ALSO
753: .Xr lex 1 ,
1.20 jmc 754: .Xr printf 1 ,
1.16 jmc 755: .Xr sed 1 ,
1.23 jmc 756: .Xr re_format 7 ,
757: .Xr script 7
1.7 aaron 758: .Rs
759: .%A A. V. Aho
760: .%A B. W. Kernighan
761: .%A P. J. Weinberger
762: .%T The AWK Programming Language
763: .%I Addison-Wesley
764: .%D 1988
765: .%O ISBN 0-201-07981-X
766: .Re
1.26 jmc 767: .Sh STANDARDS
768: The
769: .Nm
770: utility is compliant with the
1.33 jmc 771: .St -p1003.1-2008
1.26 jmc 772: specification.
773: .Pp
774: The flags
775: .Op Fl \&dV
776: and
777: .Op Fl safe ,
1.30 jmc 778: as well as the commands
779: .Cm fflush , compl , and , or ,
780: .Cm xor , lshift , rshift ,
1.26 jmc 781: are extensions to that specification.
1.32 jmc 782: .Pp
783: .Nm
784: does not support {n,m} pattern matching.
1.8 aaron 785: .Sh HISTORY
1.13 millert 786: An
1.8 aaron 787: .Nm
1.13 millert 788: utility appeared in
789: .At v7 .
1.7 aaron 790: .Sh BUGS
1.1 tholo 791: There are no explicit conversions between numbers and strings.
792: To force an expression to be treated as a number add 0 to it;
793: to force it to be treated as a string concatenate
1.7 aaron 794: .Li \&""
795: to it.
796: .Pp
1.1 tholo 797: The scope rules for variables in functions are a botch;
798: the syntax is worse.