awk supports functions and comes with a collection of built-in and useful functions.
https://www.gnu.org/software/gawk/manual/html_node/User_002ddefined.html#User_002ddefined
function name( [parm-list] ) { actions [return argument] }
#f.awk # For demo, use echo "" | awk -f f.awk # Note that awk uses a 2 pass process, and functions # can be declared later in the script but are available # earlier in the script. # # The main block of code, the driver awk script is referred to # in comments as main. # BEGIN { # no space between function and ( num=7; ary[1]=5; print "in main num=", num; print "and ary[1]=", ary[1]; # scalar passed by value and array passed by reference ans=ask(num, ary, "boy" ); # num not changed in main, ary is # and ans contains the 'changed' num value print "in main num=", num, " and ans=", ans; print "and ary[1]=", ary[1]; # name is created inside function amd continues outside # lname was a 'local' declared in the declaration, # not passed out print name, lname; } # By passing lname and num, scalars, in, they become local in the function. # Because ary is an array, it is passed by reference, which means not local. # Howowever, name, not previously defined, becomes a global scalar value. # # A function can return a single argument (string) # If a function is to 'return' multiple values, use an array created in # main and pass it in. The function can then modify elements of the array # which will then be available when function completes. # function ask (num, ary, lname) { print "lname in", lname; name="tim" lname="tank" var="in ask, num="; print var, num; num=4 print "Num tweaked=", num print "local var with value passed in", lname ary[1]=3 # only one argument can be returned # if two or more 'arguments' provided, they are # concatenated. return num 1 } |
Output :
in main num= 7 and ary[1]= 5 lname in boy in ask, num= 7 Num tweaked= 4 local var with value passed in tank in main num= 7 and ans= 41 and ary[1]= 3 tim |
rand() - returns a random value 0 <= r < 1.
exp(x) - returns e^x.
sqrt(x) - returns positive square root.
Misc:
int(x) - returns integer portion of a number.
regexp - uses // to delimit a regular expression. If looking for /, use backslash to escape, \/
When looking for a string constant, use quotes, "".
replacement - string to replace with. & represents exact match found and can be used to duplicate. sub(/dog/, "& and cat")
target - field to target rather than whole record.
sub(/dog/, "& and cat", $3 )
Arguments same as sub
Regex, replacement, and target arguments same as sub
how - specifies which occurrence in target, g and G indicate all, a numeric value indicates specific occurrence, so 2 is only 2nd occurrence.
,
string may be constant, variable or field.
regexp may be /.../ or "..."
[loc =] optional, assigned location or zero if no match. match will assigns results to two predefined variables.
RLENGTH - length of match or -1 of no match.
Works like fgrep but return index of first character of match.
in can be string, variable of field.
find must be string constant.
array - array to store string elements in.
[, fieldsep - optional field separator. May be string constant ".."
or regexp, /.../.
If unspecified, uses FS.
"", null will separate every character.
[, seps]] - separate array of field separators.
[count = ] - returns count of elements created.
string - string to split, can be constant, variable, or field.
array - array to store string elements in.
[, field-pat - optional field pattern. May be string constant ".."
or regexp, /.../.
If unspecified, uses FPAT.
[, seps]] - separate list of field separators.
[count = ] - returns count of elements created.
Example. a file contains lines of 3 digit numbers with no spacing.
tcount=pat-split( $0, nums, /[0-9][0-9][0-9]/ )
Looks for leading 0 (zero) to indicate octal, and leading 0x or 0X to indicate hexadecimal.
for additional predefined functions.