|
|
1.1 ! root 1: .TH AWK 1 ! 2: .CT 1 files prog_other ! 3: .SH NAME ! 4: awk \- pattern-directed scanning and processing language ! 5: .SH SYNOPSIS ! 6: .B awk ! 7: [ ! 8: .BI -F fs ! 9: ] ! 10: [ ! 11: .BI -v ! 12: .I var=value ! 13: ] ... ! 14: [ ! 15: .B -f ! 16: ] ! 17: .I prog ! 18: [ ! 19: .I file ... ! 20: ] ! 21: .SH DESCRIPTION ! 22: .I Awk ! 23: scans each input ! 24: .I file ! 25: for lines that match any of a set of patterns in ! 26: .IR prog , ! 27: which may appear as one literal argument or as one or more ! 28: file names each preceded by ! 29: .BR -f . ! 30: With each pattern ! 31: may be associated an action to be performed ! 32: when a line of a ! 33: .I file ! 34: matches the pattern. ! 35: Each line is matched against the ! 36: pattern portion of every pattern-action statement in order; ! 37: the associated action is performed for each matched pattern. ! 38: The file name ! 39: .L - ! 40: means the standard input. ! 41: Any ! 42: .IR file ! 43: of the form ! 44: .I var=value ! 45: is treated as an assignment, not a filename, ! 46: and is executed at the time it would have been opened if it were a filename. ! 47: Option ! 48: .B -v ! 49: designates an assignment to be done before ! 50: .I prog ! 51: is executed. ! 52: .PP ! 53: An input line is made up of fields separated by white space, ! 54: or by regular expression ! 55: .BR FS . ! 56: The fields are denoted ! 57: .BR $1 , ! 58: .BR $2 , ! 59: \&..., while ! 60: .B $0 ! 61: refers to the entire line. ! 62: .PP ! 63: A pattern-action statement has the form ! 64: .IP ! 65: .IB pattern " { " action " } ! 66: .PP ! 67: A missing ! 68: .BI { " action " } ! 69: means print the line; ! 70: a missing pattern always matches. ! 71: Pattern-action statements are separated by newlines or semicolons. ! 72: .PP ! 73: An action is a sequence of statements. ! 74: A statement can be one of the following: ! 75: .PP ! 76: .EX ! 77: .ta \w'\f(CWdelete array[expression]'u ! 78: if(\fI expression \fP)\fI statement \fP\fR[ \fPelse\fI statement \fP\fR]\fP ! 79: while(\fI expression \fP)\fI statement\fP ! 80: for(\fI expression \fP;\fI expression \fP;\fI expression \fP)\fI statement\fP ! 81: for(\fI var \fPin\fI array \fP)\fI statement\fP ! 82: do\fI statement \fPwhile(\fI expression \fP) ! 83: break ! 84: continue ! 85: {\fR [\fP\fI statement ... \fP\fR] \fP} ! 86: \fIexpression\fP #\fR commonly\fP\fI var = expression\fP ! 87: print\fR [ \fP\fIexpression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP ! 88: printf\fI format \fP\fR[ \fP,\fI expression-list \fP\fR] \fP\fR[ \fP>\fI expression \fP\fR]\fP ! 89: return\fR [ \fP\fIexpression \fP\fR]\fP ! 90: next #\fR skip remaining patterns on this input line\fP ! 91: delete\fI array\fP[\fI expression \fP] #\fR delete an array element\fP ! 92: exit\fR [ \fP\fIexpression \fP\fR]\fP #\fR exit immediately; status is \fP\fIexpression\fP ! 93: .EE ! 94: .DT ! 95: .PP ! 96: Statements are terminated by ! 97: semicolons, newlines or right braces. ! 98: An empty ! 99: .I expression-list ! 100: stands for ! 101: .BR $0 . ! 102: String constants are quoted \&\f(CW"\ "\fR, ! 103: with the usual C escapes recognized within. ! 104: Expressions take on string or numeric values as appropriate, ! 105: and are built using the operators ! 106: .B + - * / % ^ ! 107: (exponentiation), and concatenation (indicated by white space). ! 108: The operators ! 109: .B ! 110: ! ++ -- += -= *= /= %= ^= > >= < <= == != && || ?: ! 111: are also available in expressions. ! 112: Variables may be scalars, array elements ! 113: (denoted ! 114: .IB x [ i ] ) ! 115: or fields. ! 116: Variables are initialized to the null string. ! 117: Array subscripts may be any string, ! 118: not necessarily numeric; ! 119: this allows for a form of associative memory. ! 120: Multiple subscripts such as ! 121: .B [i,j,k] ! 122: are permitted; the constituents are concatenated, ! 123: separated by the value of ! 124: .BR SUBSEP . ! 125: .PP ! 126: The ! 127: .B print ! 128: statement prints its arguments on the standard output ! 129: (or on a file if ! 130: .BI > file ! 131: or ! 132: .BI >> file ! 133: is present or on a pipe if ! 134: .BI | cmd ! 135: is present), separated by the current output field separator, ! 136: and terminated by the output record separator. ! 137: .I file ! 138: and ! 139: .I cmd ! 140: may be literal names or parenthesized expressions; ! 141: identical string values in different statements denote ! 142: the same open file. ! 143: The ! 144: .B printf ! 145: statement formats its expression list according to the format ! 146: (see ! 147: .IR printf (3)) . ! 148: The built-in function ! 149: .BI close( expr ) ! 150: closes the file or pipe ! 151: .IR expr . ! 152: .PP ! 153: The mathematical functions ! 154: .BR exp , ! 155: .BR log , ! 156: .BR sqrt , ! 157: .BR sin , ! 158: .BR cos , ! 159: and ! 160: .BR atan2 ! 161: are built in. ! 162: Other built-in functions: ! 163: .TF length ! 164: .TP ! 165: .B length ! 166: the length of its argument ! 167: taken as a string, ! 168: or of ! 169: .B $0 ! 170: if no argument. ! 171: .TP ! 172: .B rand ! 173: random number on (0,1) ! 174: .TP ! 175: .B srand ! 176: sets seed for ! 177: .B rand ! 178: and returns the previous seed. ! 179: .TP ! 180: .B int ! 181: truncates to an integer value ! 182: .TP ! 183: .BI substr( s , " m" , " n\fB) ! 184: the ! 185: .IR n -character ! 186: substring of ! 187: .I s ! 188: that begins at position ! 189: .IR m ! 190: counted from 1. ! 191: .TP ! 192: .BI index( s , " t" ) ! 193: the position in ! 194: .I s ! 195: where the string ! 196: .I t ! 197: occurs, or 0 if it does not. ! 198: .TP ! 199: .BI match( s , " r" ) ! 200: the position in ! 201: .I s ! 202: where the regular expression ! 203: .I r ! 204: occurs, or 0 if it does not. ! 205: The variables ! 206: .B RSTART ! 207: and ! 208: .B RLENGTH ! 209: are set to the position and length of the matched string. ! 210: .TP ! 211: .BI split( s , " a" , " fs\fB) ! 212: splits the string ! 213: .I s ! 214: into array elements ! 215: .IB a [1] , ! 216: .IB a [2] , ! 217: \&..., ! 218: .IB a [ n ] , ! 219: and returns ! 220: .IR n . ! 221: The separation is done with the regular expression ! 222: .I fs ! 223: or with the field separator ! 224: .B FS ! 225: if ! 226: .I fs ! 227: is not given. ! 228: .TP ! 229: .BI sub( r , " t" , " s\fB) ! 230: substitutes ! 231: .I t ! 232: for the first occurrence of the regular expression ! 233: .I r ! 234: in the string ! 235: .IR s . ! 236: If ! 237: .I s ! 238: is not given, ! 239: .B $0 ! 240: is used. ! 241: .TP ! 242: .B gsub ! 243: same as ! 244: .B sub ! 245: except that all occurrences of the regular expression ! 246: are replaced; ! 247: .B sub ! 248: and ! 249: .B gsub ! 250: return the number of replacements. ! 251: .TP ! 252: .BI sprintf( fmt , " expr" , " ...\fB ) ! 253: the string resulting from formatting ! 254: .I expr ... ! 255: according to the ! 256: .IR printf (3) ! 257: format ! 258: .I fmt ! 259: .TP ! 260: .BI system( cmd ) ! 261: executes ! 262: .I cmd ! 263: and returns its exit status ! 264: .PD ! 265: .PP ! 266: The ``function'' ! 267: .B getline ! 268: sets ! 269: .B $0 to ! 270: the next input record from the current input file; ! 271: .B getline ! 272: .BI < file ! 273: sets ! 274: .B $0 ! 275: to the next record from ! 276: .IR file . ! 277: .B getline ! 278: .I x ! 279: sets variable ! 280: .I x ! 281: instead. ! 282: Finally, ! 283: .IB cmd " | getline ! 284: pipes the output of ! 285: .I cmd ! 286: into ! 287: .BR getline ; ! 288: each call of ! 289: .B getline ! 290: returns the next line of output from ! 291: .IR cmd . ! 292: In all cases, ! 293: .B getline ! 294: returns 1 for a successful input, ! 295: 0 for end of file, and \-1 for an error. ! 296: .PP ! 297: Patterns are arbitrary Boolean combinations ! 298: (with ! 299: .BR "! || &&" ) ! 300: of regular expressions and ! 301: relational expressions. ! 302: Regular expressions are as in ! 303: .IR egrep ; ! 304: see ! 305: .IR gre (1). ! 306: Isolated regular expressions ! 307: in a pattern apply to the entire line. ! 308: Regular expressions may also occur in ! 309: relational expressions, using the operators ! 310: .BR ~ ! 311: and ! 312: .BR !~ . ! 313: .BI / re / ! 314: is a constant regular expression; ! 315: any string (constant or variable) may be used ! 316: as a regular expression, except in the position of an isolated regular expression ! 317: in a pattern. ! 318: .PP ! 319: A pattern may consist of two patterns separated by a comma; ! 320: in this case, the action is performed for all lines ! 321: from an occurrence of the first pattern ! 322: though an occurrence of the second. ! 323: .PP ! 324: A relational expression is one of the following: ! 325: .IP ! 326: .I expression matchop regular-expression ! 327: .br ! 328: .I expression relop expression ! 329: .br ! 330: .IB expression " in " array-name ! 331: .br ! 332: .BI ( expr , expr,... ") in " array-name ! 333: .PP ! 334: where a relop is any of the six relational operators in C, ! 335: and a matchop is either ! 336: .B ~ ! 337: (matches) ! 338: or ! 339: .B !~ ! 340: (does not match). ! 341: A conditional is an arithmetic expression, ! 342: a relational expression, ! 343: or a Boolean combination ! 344: of these. ! 345: .PP ! 346: The special patterns ! 347: .B BEGIN ! 348: and ! 349: .B END ! 350: may be used to capture control before the first input line is read ! 351: and after the last. ! 352: .B BEGIN ! 353: and ! 354: .B END ! 355: do not combine with other patterns. ! 356: .PP ! 357: Variable names with special meanings: ! 358: .TF FILENAME ! 359: .TP ! 360: .B FS ! 361: regular expression used to separate fields; also settable ! 362: by option ! 363: .BI -F fs. ! 364: .TP ! 365: .BR NF ! 366: number of fields in the current record ! 367: .TP ! 368: .B NR ! 369: ordinal number of the current record ! 370: .TP ! 371: .B FNR ! 372: ordinal number of the current record in the current file ! 373: .TP ! 374: .B FILENAME ! 375: the name of the current input file ! 376: .TP ! 377: .B RS ! 378: input record separator (default newline) ! 379: .TP ! 380: .B OFS ! 381: output field separator (default blank) ! 382: .TP ! 383: .B ORS ! 384: output record separator (default newline) ! 385: .TP ! 386: .B OFMT ! 387: output format for numbers (default ! 388: .BR "%.6g" ) ! 389: .TP ! 390: .B SUBSEP ! 391: separates multiple subscripts (default 034) ! 392: .TP ! 393: .B ARGC ! 394: argument count, assignable ! 395: .TP ! 396: .B ARGV ! 397: argument array, assignable; ! 398: non-null members are taken as filenames ! 399: .TP ! 400: .B ENVIRON ! 401: array of environment variables; subscripts are names. ! 402: .PD ! 403: .PP ! 404: Functions may be defined (at the position of a pattern-action statement) thus: ! 405: .IP ! 406: .L ! 407: function foo(a, b, c) { ...; return x } ! 408: .PP ! 409: Parameters are passed by value if scalar and by reference if array name; ! 410: functions may be called recursively. ! 411: Parameters are local to the function; all other variables are global. ! 412: Thus local variables may be created by providing excess parameters in ! 413: the function definition. ! 414: .SH EXAMPLES ! 415: .TP ! 416: .L ! 417: length > 72 ! 418: Print lines longer than 72 characters. ! 419: .TP ! 420: .L ! 421: { print $2, $1 } ! 422: Print first two fields in opposite order. ! 423: .PP ! 424: .EX ! 425: BEGIN { FS = ",[ \et]*|[ \et]+" } ! 426: { print $2, $1 } ! 427: .EE ! 428: .ns ! 429: .IP ! 430: Same, with input fields separated by comma and/or blanks and tabs. ! 431: .PP ! 432: .EX ! 433: { s += $1 } ! 434: END { print "sum is", s, " average is", s/NR } ! 435: .EE ! 436: .ns ! 437: .IP ! 438: Add up first column, print sum and average. ! 439: .TP ! 440: .L ! 441: /start/, /stop/ ! 442: Print all lines between start/stop pairs. ! 443: .PP ! 444: .EX ! 445: BEGIN { # Simulate echo(1) ! 446: for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i] ! 447: printf "\en" ! 448: exit } ! 449: .EE ! 450: .SH SEE ALSO ! 451: .IR gre (1), ! 452: .IR lex (1), ! 453: .IR sed (1) ! 454: .br ! 455: A. V. Aho, B. W. Kernighan, P. J. Weinberger, ! 456: .I ! 457: The AWK Programming Language, ! 458: Addison-Wesley, 1988. ! 459: .SH BUGS ! 460: There are no explicit conversions between numbers and strings. ! 461: To force an expression to be treated as a number add 0 to it; ! 462: to force it to be treated as a string concatenate ! 463: \&\f(CW""\fP to it. ! 464: .br ! 465: The scope rules for variables in functions are a botch; ! 466: the syntax is worse.
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.