gawk: Function Example

1 
1 9.2.2 Function Definition Examples
1 ----------------------------------
1 
1 Here is an example of a user-defined function, called 'myprint()', that
1 takes a number and prints it in a specific format:
1 
1      function myprint(num)
1      {
1           printf "%6.3g\n", num
1      }
1 
1 To illustrate, here is an 'awk' rule that uses our 'myprint()' function:
1 
1      $3 > 0     { myprint($3) }
1 
1 This program prints, in our special format, all the third fields that
1 contain a positive number in our input.  Therefore, when given the
1 following input:
1 
1       1.2   3.4    5.6   7.8
1       9.10 11.12 -13.14 15.16
1      17.18 19.20  21.22 23.24
1 
1 this program, using our function to format the results, prints:
1 
1         5.6
1        21.2
1 
1    This function deletes all the elements in an array (recall that the
1 extra whitespace signifies the start of the local variable list):
1 
1      function delarray(a,    i)
1      {
1          for (i in a)
1              delete a[i]
1      }
1 
1    When working with arrays, it is often necessary to delete all the
11 elements in an array and start over with a new list of elements (⇒
 Delete).  Instead of having to repeat this loop everywhere that you
1 need to clear out an array, your program can just call 'delarray()'.
1 (This guarantees portability.  The use of 'delete ARRAY' to delete the
1 contents of an entire array is a relatively recent(1) addition to the
1 POSIX standard.)
1 
1    The following is an example of a recursive function.  It takes a
1 string as an input parameter and returns the string in reverse order.
1 Recursive functions must always have a test that stops the recursion.
1 In this case, the recursion terminates when the input string is already
1 empty:
1 
1      function rev(str)
1      {
1          if (str == "")
1              return ""
1 
1          return (rev(substr(str, 2)) substr(str, 1, 1))
1      }
1 
1    If this function is in a file named 'rev.awk', it can be tested this
1 way:
1 
1      $ echo "Don't Panic!" |
1      > gawk -e '{ print rev($0) }' -f rev.awk
1      -| !cinaP t'noD
1 
1    The C 'ctime()' function takes a timestamp and returns it as a
1 string, formatted in a well-known fashion.  The following example uses
1 the built-in 'strftime()' function (⇒Time Functions) to create an
1 'awk' version of 'ctime()':
1 
1      # ctime.awk
1      #
1      # awk version of C ctime(3) function
1 
1      function ctime(ts,    format)
1      {
1          format = "%a %b %e %H:%M:%S %Z %Y"
1 
1          if (ts == 0)
1              ts = systime()       # use current time as default
1          return strftime(format, ts)
1      }
1 
1    You might think that 'ctime()' could use 'PROCINFO["strftime"]' for
1 its format string.  That would be a mistake, because 'ctime()' is
1 supposed to return the time formatted in a standard fashion, and
1 user-level code could have changed 'PROCINFO["strftime"]'.
1 
1    ---------- Footnotes ----------
1 
1    (1) Late in 2012.
1