# sum.awk -- summary of awk(1) # $Id$ # Carlos Duarte , 980630 Flow control and command syntax function fn(parameters) { action } pattern { action } pattern: BEGIN before read first line END after last is read and processed re or relation selector . ! || && may be used for combining re's and relations actions: # comment (upto end of line) if (expr) stmt1 [else stmt2] while (expr) stmt for ([expr1]; [expr2]; [expr3]) stmt for (var in array) stmt do stmt while (expr) break step out current loop continue continue to next iteration of a loop expr typ expr: x = y { [stmt(s)] } return [expr] return `expr' from function next restart script with next record loaded nextfile restart script with next file loaded exit [expr] at record patterns, exits to END; at END exits program with status `expr' delete A[expr] delete one element of A delete A delete all elements of A Operators and precedence Assc. Operator Description L () [] group exprs, dereference arrays L $ field reference L ++ -- increment, decrement R ^ exponentiation L + - ! unaries plus, minus; logical negation L * / % mul, div, mod L + - add, sub L space concatenation L < <= >= > != == relational operators L ~ !~ re match; re not match L in presence in array L && logical AND L || logical OR R ?: ternary conditional operator R = += -= *= /= %= ^= set; do operation, and set L: associativity left to right R: associativity right to left precedence decreases to down Recognized escapes in strings or regular expressions esc desc dec oct hex \0 nul 0 000 00 null character \a bel 7 007 07 alert \b bs 8 010 08 backspace \t ht 9 011 09 horizontal tab \n lf 10 012 0A newline \v vt 11 013 0B vertical tab \f ff 12 014 0C formfeed \r cr 13 015 0D carriage return \\ \ 92 134 5C backslash \" " 34 042 22 double quote \ooo oct ooo - ooo - octal value \xhh hex hh - - hh hex value Regular expressions special chars ^ $ () * + . ? [ ] | literal chars all others Atoms (indivisible pieces of re's): c if literal, matches itself \c if escape, matches the escape, else `c' is literal . any char ^ begin of buffer $ end of buffer [list] any char on `list' (list= seq: abcd; range: a-z) [^list] any char _not_ on `list' Compounds: r1r2 matches `r1' iff followed per `r2' r1|r2 matches `r1' or `r2' r* 0 or more occurrences of `r' r+ 1 or more occurrences of `r' (equiv: rr*) r? 0 or 1 occurrence of `r' (r) groups `r' Built-in math functions int(x) returns the nearest integer of `x', towards 0 (remove fractional part) exp(x) exponential of `x' (e^x) log(x) natural (base e) logarithm of x: y=log(x), s.a e^y=x sqrt(x) returns square root of `x' [these operate/return radians] sin(x) return sine of `x' cos(x) return cosine of `x' atan2(x) return arctan of y/x rand() return a float random number, on ]0,1[ srand(expr) set seed as `expr' srand() set seed as current date and time Built-in string functions gsub(re,s,t) replace all occurrences of `re' on `t', per `s'; return number of replacements done; gsub(re,s) same as gsub(re,s,$0) sub(re,s,t) sub(re,s) like gsub(), but exec only the 1st replacement index(s,t) return position of `t' in `s', 1 based 0 if not found match(s,re) match `re' on `s' return index, 1 based, or 0 if not found length(s) length of `s' split(s,A,re) split `s' into k fields separated per `re'; fields are placed at A[1]...A[k] returns k split(s,A) same as split(s,A,FS) sprintf(fmt[, expr, ...]) returns the string built per `fmt' and `expr'... substr(s,i,n) returns substring of `s', that starts at `i' (1 based) with length `n' substr(s,i) same as substr(s,i,length(s)-i+1) tolower(s) returns a lowercased version of `s' toupper(s) returns a uppercased version of `s' I/O support getline var read next input record into `var' getline var file write to file (destroy contents at start) pr >> file append to file pr | cmd write to pipe (which is read per `cmd') system(cmd) execute `cmd' on an external shell fflush("") flushes all files and pipes fflush() flushes stdout fflush(file) flushes `file' close(file) close `file' Predefined variables ARGC number of command-line arguments ARGV command line arguments, ARGV[0]..ARGV[ARGC-1] OFMT format for printing numbers; default "%.6g" CONVFMT fmt of implicit cinverson number -> string; def: "%.6g" ENVIRON array of external environ; ENVIRON[var] = value FILENAME current input file FNR current file record number FS input field separator, as a regular expression RS input record separator; def: "\n" OFS output field separator; def: " " ORS output record separator; def: "\n" NF number of fields in current record NR current record number of input stream RSTART index set by last `match' RLENGTH length of last `match' found regular expression SUBSEP separator for multiple dimension arrays; def: "\034"