auk provides a mechanism for user defined variables.
Variable name are composed of alpha characters, numeric characters, and underscore.
Do not start a variable name with numbers.
Names are case sensitive.
Do not use hyphen, it will be treated as a minus.
Variables are cast free :
can be assigned an integer.
can be assigned an float.
can be assigned an string.
Name length ? - probably anything < 32 characters OK.
These variables may be :
Mawk types variables as numeric, string, or numeric and string, but appear to store the values as the characters assigned.
Variables are evaluated when used, so if valid number, can be used as such.
Make sure you don't already have a file called d and then try the following :
echo "-7 +4.5 0.3178 6.02e23" > d awk "{ print length( $1 ); }" d |
The it will print out :
2
4
6
7
Because awk is an interpreted language, the possible length of the value stored is large, most likely limited by system limits such as length of command line (4096 characters?).
This does not guarantee that some action applied to the variable will work if it has a long value.
Scalars are assigned value with the single equal, =.
Scalars are referenced by just using their name, $ not required.
myVar="This is a value"
print myVar
Dereferencing works only with the predefined variables such as NR, NF, etc.
Operators
Assignment Operators - performs operation and assigns result to variable.
num1=7 print num1++, num1, ++num1 # output 7 8 9 |
Arrays in awk are actually associative. The index is just a key string value that is a number.
awk supports multi-dimensional arrays.
awk arrays are not rectangular.
- a 4 x 3 2-d array does not have 12 elements unless they have actually been
assigned.
Elements in an array are assigned a value or referenced with [].
An array does not have to be pre-declared.
If an non-existent array element is referenced, it will be created.
But a variable name already in use CANNOT be reused as a different type.
One dimensional array :
nums[7] = 42; print nums[7];
Pseudo - two dimensional array :
manynums[4,3] = 19;
awk concatenates the keys or indexes into a pair of values separated by the ASCII field separator (FS) value, Octal 034, decimal 28, hex 1C.
Normally, the FS is non-printable, but if you pipe the output through less, you will see there is a separator.
Current versions of awk support true multidimensional arrays or arrays of arrays.
arrays_of_nums[4][3] = 19;
An awk script, ary.awk, contains the following :
{ pary[$3,$2] = $8 tary[$3][$2] = $8 } END { # pseudo 2-d array. Print "Pseudo array" for ( k in pary ) { print k print pary[k] } # true array of arrays. print " " print "True array" for ( k in tary ) { print k for ( l in tary[k] ) { print l, tary[k][l] } } } |
ps -ef | awk -f ary.awk
Associative arrays use a key string to identify an element of the array.
users["bob"] = "William Tell"; users["gracey"] = "Grade Hopper"; expenses[$1]=$2 # uses 1st field as the key to a list of values in 2nd field.
#sort.awk BEGIN { print "As read in"; } # slightly modified from man page on mawk # read in a table of expense-category and expense-values into an # associative array keyed on expense-category. # This will total costs, 2nd column, for each expense catagory, 1st column # and display each record. { costs[$1]+=$2; print $1, $2; } END { lcount=length(costs); print NR, lcount; print "\nAs read from array and initialized"; # copy the keys from the associative array into another 'indexed' array. ndx=1; for ( key in costs ) { print key, costs[key]; keylist[ndx]=key; ndx++; } # sort the indexed array. This will alphabetize the keys. isort(keylist, lcount) print "\nsorted" # Now print the results. for(i = 1 ; i <= lcount; i++) { print keylist[i], costs[keylist[i]]; } } #insertion sort of A[1..n] function isort( A, n, i, j, hold) { for( i = 2 ; i <= n ; i++) { hold = A[j = i] while ( A[j-1] > hold ) { j-- ; A[j+1] = A[j] } A[j] = hold } # sentinel A[0] = "" will be created if needed } |
fuel 23.79 car 443.35 gas 12.23 water 48.75 electric 47.23 fuel 19.34 TV 124.73 phone 103.73 fuel 23.22 gas 3.03 water 49.57 electric 67.88 fuel 18.43 TV 124.73 phone 103.73 fuel 19.12