7.2 awk operators, builtin variables and functions
7. Operators
~ match regular expression || logical OR
!~ don't match regular expression && logical AND
< less than ! logical NOT
<= less than or equal
> greater than + addition
>= greater than or equal - subtraction
!= not identical to / division
== identical to * multiplication
' ' concatenate % remainder
compound assignments:
+= compound add (x = x + n) ++ autoincrement
-= compound subtract (x = x - n) -- autodecrement
*= compound multiply (x = x * n)
/= compound divide (x = x / n) sub in array is subscript in array?
%= compound remainder (x = x % n)
8. Built-in variables
$n field number n on each line
$0 the entire line of input
FILENAME name of the current file being read
NR Number of Records (lines) read so far
FNR Record number in the current file
NF Number of Fields in the current Record (line)
RS Input Record separator
FS Input Field separator
ORS Output Record separator
OFS Output Field separator
OFMT Output format for floating point numbers ( %.6g)
9. Built-in functions
length(string) returns the length of the string
substr(string,st,k) returns the substring of string
starting at position st and of
length k
index(str1, str2) returns the position of string str2
in str1. If not found, returns 0
sprintf(frmt, exprlist) returns a string formatted according
to the format frmt using values in
the exprlist
int(num) returns the truncated integer value
of num
cos(x) returns the cosine of x (in radians)
sin(x) returns the sine of x
atan(y,x) returns the arctangent of y/x
log(x) returns natural log of x
exp(x) returns exponential e to the x
sqrt(x) returns the square root of x
rand() returns random number 0<=r<1
srand([x]) uses x as a new seed for rand
system("unix command") execute a unix command in a subshell
function name(param[,param]...) { [statement] ... }
defines a function
wherever a pattern
action pair goes.
Questions? Robert Katz: katz@ned.highline.edu
Last Update April 7, 2005