### Initialization of a scalar cookie Source: https://www.gnu.org/software/gawk/manual/html_node/Symbol-table-by-cookie.html This example shows how to install a variable and retrieve its scalar cookie during extension initialization for later use. ```c static awk_scalar_t magic_var_cookie; /* cookie for MAGIC_VAR */ static void my_extension_init() { awk_value_t value; ``` ```c /* install initial value */ sym_update("MAGIC_VAR", make_number(42.0, & value)); /* get the cookie */ sym_lookup("MAGIC_VAR", AWK_SCALAR, & value); /* save the cookie */ magic_var_cookie = value.scalar_cookie; ... } ``` -------------------------------- ### POSIX date utility example Source: https://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html An example of using the date utility with a format string starting with a plus sign. ```shell $ `date '+Today is %A, %B %d, %Y.'` -| Today is Monday, September 22, 2014. ``` -------------------------------- ### Switch statement example with getopt Source: https://www.gnu.org/software/gawk/manual/html_node/Switch-Statement.html An example demonstrating the use of a switch statement to handle command-line options parsed by getopt. ```awk while ((c = getopt(ARGC, ARGV, "aksx")) != -1) { switch (c) { case "a": # report size of all files all_files = TRUE; break case "k": BLOCK_SIZE = 1024 # 1K block size break case "s": # do sums only sum_only = TRUE break case "x": # don't cross filesystems fts_flags = or(fts_flags, FTS_XDEV) break case "?": default: usage() break } } ``` -------------------------------- ### Build and install gawk Source: https://www.gnu.org/software/gawk/manual/html_node/gawkextlib.html Commands to configure, build, check, and install gawk into /tmp/newgawk. ```bash cd .../path/to/gawk/code ./configure --prefix=/tmp/newgawk _Install in /tmp/newgawk for now_ make && make check _Build and check that all is OK_ make install _Install gawk_ ``` -------------------------------- ### Shell command example with output and error Source: https://www.gnu.org/software/gawk/manual/html_node/Conventions.html Shows how command-line examples are presented: input is preceded by the shell prompt '$', standard output by '-|', and standard error by 'error→'. The example demonstrates printing to stdout and redirecting to stderr. ```shell $ `echo hi on stdout` -| hi on stdout $ `echo hello on stderr 1>&2` error→ hello on stderr ``` -------------------------------- ### Example awk program file (advice) Source: https://www.gnu.org/software/gawk/manual/html_node/Long.html This is the content of the file 'advice' used in the example. It prints the message 'Don't Panic!' when run. ```awk BEGIN { print "Don't Panic!" } ``` -------------------------------- ### Output of myprint() example Source: https://www.gnu.org/software/gawk/manual/html_node/Function-Example.html The output produced by the myprint() example when given the sample input. It shows the formatted positive third fields. ```text 5.6 21.2 ``` -------------------------------- ### Sample data file Source: https://www.gnu.org/software/gawk/manual/html_node/Indirect-Calls.html Example input file format for the sorting program. ```text Biology_101 sum average sort rsort data: 87.0 92.4 78.5 94.9 Chemistry_305 sum average sort rsort data: 75.2 98.3 94.7 88.2 English_401 sum average sort rsort data: 100.0 95.6 87.1 93.4 ``` -------------------------------- ### Install package from source Source: https://www.gnu.org/software/gawk/manual/html_node/Derived-Files.html Commands to download, extract, build, and install a GNU package from source. The note mentions that the --no-check-certificate option may be needed for wget due to the https:// URL. ```bash wget https://ftp.gnu.org/gnu/package/package-x.y.z.tar.gz tar -xpzvf package-x.y.z.tar.gz cd package-x.y.z ./configure && make && make check make install # as root ``` -------------------------------- ### Example array contents for asort/asorti Source: https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html Initial contents of array 'a' used in the asort and asorti examples. Shows three key-value pairs with string values. ```awk a["last"] = "de" a["first"] = "sac" a["middle"] = "cul" ``` -------------------------------- ### Sample Test Program Runs Source: https://www.gnu.org/software/gawk/manual/html_node/Getopt-Function.html Example command-line executions and their corresponding outputs for the getopt test program. ```bash $ `awk -f getopt.awk -v _getopt_test=1 -- -a -cbARG bax -x` -| c = , Optarg = <> -| c = , Optarg = <> -| c = , Optarg = -| non-option arguments: -| ARGV[3] = -| ARGV[4] = <-x> $ `awk -f getopt.awk -v _getopt_test=1 -- -a -x -- xyz abc` -| c = , Optarg = <> error→ x -- invalid option -| c = , Optarg = <> -| non-option arguments: -| ARGV[4] = -| ARGV[5] = $ `awk -f getopt.awk -v _getopt_test=1 -- -a \ > `--longa -b xx --longb=foo=bar --otherd --otherc arg1 arg2` -| c = , Optarg = <> -| c = , Optarg = <> -| c = , Optarg = -| c = , Optarg = -| c = , Optarg = <> -| c = , Optarg = <> -| non-option arguments: -| ARGV[8] = -| ARGV[9] = ``` -------------------------------- ### Example usage of ord() and chr() Source: https://www.gnu.org/software/gawk/manual/html_node/Extension-Sample-Ord.html Demonstrates loading the extension and using ord() to get the numeric value of a character and chr() to get the character from a numeric value. ```gawk @load "ordchr" ... printf("The numeric value of 'A' is %d\n", ord("A")) printf("The string value of 65 is %s\n", chr(65)) ``` -------------------------------- ### Initialize getopt and Test Harness Source: https://www.gnu.org/software/gawk/manual/html_node/Getopt-Function.html Sets up the initial state for getopt and provides a test block to demonstrate option parsing. ```awk BEGIN { Opterr = 1 # default is to diagnose Optind = 1 # skip ARGV[0] # test program if (_getopt_test) { _myshortopts = "ab:cd" _mylongopts = "longa,longb:,otherc,otherd" while ((_go_c = getopt(ARGC, ARGV, _myshortopts, _mylongopts)) != -1) printf("c = <%s>, Optarg = <%s>\n", _go_c, Optarg) printf("non-option arguments:\n") for (; Optind < ARGC; Optind++) printf("\tARGV[%d] = <%s>\n", Optind, ARGV[Optind]) } } ``` -------------------------------- ### split() example: split string into array and separators Source: https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html Splits the string "cul-de-sac" using '-' as separator. The array 'a' gets the pieces and 'seps' gets the separator strings. Returns 3. ```awk split("cul-de-sac", a, "-", seps) ``` ```awk a[1] = "cul" a[2] = "de" a[3] = "sac" ``` ```awk seps[1] = "-" seps[2] = "-" ``` -------------------------------- ### View all configure options Source: https://www.gnu.org/software/gawk/manual/html_node/Additional-Configuration-Options.html Use this command to display the full list of available options provided by the configure script. ```bash ./configure --help ``` -------------------------------- ### Shell file redirection example Source: https://www.gnu.org/software/gawk/manual/html_node/Redirection.html Demonstrates the standard shell approach of using > to create a file and >> for subsequent additions. ```shell echo Name: Arnold Robbins > data echo Street Address: 1234 A Pretty Street, NE >> data echo City and State: MyTown, MyState 12345-6789 >> data ``` -------------------------------- ### awk comment example Source: https://www.gnu.org/software/gawk/manual/html_node/Comments.html Shows how to add comments to an awk program. Comments start with '#' and continue to the end of the line. The '#' does not have to be the first character on the line. ```awk # This program prints a nice, friendly message. It helps # keep novice users from being afraid of the computer. BEGIN { print "Don't Panic!" } ``` -------------------------------- ### C extension: create and populate array with subarray Source: https://www.gnu.org/software/gawk/manual/html_node/Creating-Arrays.html Complete C extension example that creates a new array, installs it in the symbol table, adds two regular elements, creates a subarray, and populates it. The leading #include directives and boilerplate declarations are omitted for brevity. ```c /* create_new_array --- create a named array */ static void create_new_array() { awk_array_t a_cookie; awk_array_t subarray; awk_value_t index, value; a_cookie = create_array(); value.val_type = AWK_ARRAY; value.array_cookie = a_cookie; if (! sym_update("new_array", & value)) printf("create_new_array: sym_update(\"new_array\") failed!\n"); a_cookie = value.array_cookie; ``` ```c (void) make_const_string("hello", 5, & index); (void) make_const_string("world", 5, & value); if (! set_array_element(a_cookie, & index, & value)) { printf("fill_in_array: set_array_element failed\n"); return; } (void) make_const_string("answer", 6, & index); (void) make_number(42.0, & value); if (! set_array_element(a_cookie, & index, & value)) { printf("fill_in_array: set_array_element failed\n"); return; } ``` ```c (void) make_const_string("subarray", 8, & index); subarray = create_array(); value.val_type = AWK_ARRAY; value.array_cookie = subarray; if (! set_array_element(a_cookie, & index, & value)) { printf("fill_in_array: set_array_element failed\n"); return; } subarray = value.array_cookie; ``` ```c (void) make_const_string("foo", 3, & index); (void) make_const_string("bar", 3, & value); if (! set_array_element(subarray, & index, & value)) { printf("fill_in_array: set_array_element failed\n"); return; } } ``` -------------------------------- ### Build Gawk from source Source: https://www.gnu.org/software/gawk/manual/html_node/Installation-summary.html The canonical build recipe for Gawk on POSIX systems using wget, tar, configure, and make. ```bash wget https://ftp.gnu.org/gnu/gawk/gawk-5.4.0.tar.gz tar -xvpzf gawk-5.4.0.tar.gz cd gawk-5.4.0 ./configure && make && make check ``` -------------------------------- ### substr(string, start [, length]) Source: https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html Returns a substring of string starting at character position start, optionally of a specified length. If length is omitted, returns the suffix from start to the end. ```APIDOC ## substr(string, start [, length]) ### Description Returns a length-character-long substring of string, starting at character number start (the first character is number one). If length is not present, returns the whole suffix of string beginning at start. If length is greater than the number of characters remaining, the whole suffix is returned. ### Syntax substr(string, start [, length]) ### Parameters - **string** (string) - Required - The string from which to extract the substring. - **start** (number) - Required - The starting character position (1-based). If start is less than one, it is treated as one. If start is greater than the number of characters in the string, the null string is returned. - **length** (number) - Optional - The number of characters to return. If length is present but less than or equal to zero, the null string is returned. ### Return Value The extracted substring. ### Examples ``` substr("washington", 5, 3) # returns "ing" substr("washington", 5) # returns "ington" ``` ### Notes - The string returned by substr() cannot be assigned. It is a mistake to attempt to change a portion of a string, e.g., substr(string, 3, 3) = "CDE" is invalid. - It is also a mistake to use substr() as the third argument of sub() or gsub(). - To replace bits and pieces of a string, combine substr() with string concatenation, e.g., string = substr(string, 1, 2) "CDE" substr(string, 6). ``` -------------------------------- ### awk: Toggle flag to print lines between START and END markers Source: https://www.gnu.org/software/gawk/manual/html_node/Boolean-Ops.html Prints lines between special bracketing lines. The variable 'interested' starts as zero (false) and is toggled with '!' when START or END is seen. Note: this program has a bug; it prints lines starting with 'END'. ```awk $1 == "START" { interested = ! interested; next } interested { print } $1 == "END" { interested = ! interested; next } ``` -------------------------------- ### Initialize getopt() function Source: https://www.gnu.org/software/gawk/manual/html_node/Getopt-Function.html The function definition and initial check for empty options and longoptions parameters. ```awk function getopt(argc, argv, options, longopts, thisopt, i, j) { if (length(options) == 0 && length(longopts) == 0) return -1 # no options given ``` -------------------------------- ### Build gawk with make Source: https://www.gnu.org/software/gawk/manual/html_node/Quick-Installation.html After running configure, type make to build an executable version of gawk. Run 'make check' to verify it works properly. ```shell make ``` -------------------------------- ### Interval Expression Examples Source: https://www.gnu.org/software/gawk/manual/html_node/Regexp-Operator-Details.html Examples of interval expressions matching specific repetition counts. ```regexp wh{3}y ``` ```regexp wh{3,5}y ``` ```regexp wh{2,}y ``` -------------------------------- ### pwcat output example Source: https://www.gnu.org/software/gawk/manual/html_node/Passwd-Functions.html Sample output from running pwcat, showing the user database in colon-separated format. The output includes root, nobody, daemon, sys, bin, and other users. ```text $ `pwcat` -| root:x:0:1:Operator:/:/bin/sh -| nobody:*:65534:65534::/: -| daemon:*:1:1::/: -| sys:*:2:2::/:/bin/csh -| bin:*:3:3::/bin: -| arnold:xyzzy:2076:10:Arnold Robbins:/home/arnold:/bin/sh -| miriam:yxaay:112:10:Miriam Robbins:/home/miriam:/bin/sh -| andy:abcca2:113:10:Andy Jacobs:/home/andy:/bin/sh ... ``` -------------------------------- ### grcat command output example Source: https://www.gnu.org/software/gawk/manual/html_node/Group-Functions.html Example output produced by running the grcat program. ```text $ `grcat` -| wheel:*:0:arnold -| nogroup:*:65534: -| daemon:*:1: -| kmem:*:2: -| staff:*:10:arnold,miriam,andy -| other:*:20: ... ``` -------------------------------- ### Increment and Decrement Operator Examples Source: https://www.gnu.org/software/gawk/manual/html_node/Increment-Ops.html Examples demonstrating the use of increment and decrement operators in AWK. ```awk b = 6 print b += b++ ``` ```awk b = 6 b += ++b + b++ print b ``` -------------------------------- ### Input list for getline example Source: https://www.gnu.org/software/gawk/manual/html_node/Getline_002fVariable.html The input data used to demonstrate the line-swapping behavior. ```text wan tew free phore ``` -------------------------------- ### Output of length() example Source: https://www.gnu.org/software/gawk/manual/html_node/Arrays-of-Arrays.html The output produced by the preceding length() example for the main array a. ```awk 2, 3, 1 ``` -------------------------------- ### Sample input for myprint() example Source: https://www.gnu.org/software/gawk/manual/html_node/Function-Example.html Sample input data used to demonstrate the myprint() function. It contains three lines of numbers. ```text 1.2 3.4 5.6 7.8 9.10 11.12 -13.14 15.16 17.18 19.20 21.22 23.24 ``` -------------------------------- ### Negative remainder example Source: https://www.gnu.org/software/gawk/manual/html_node/Arithmetic-Ops.html An example showing that the remainder operator produces a negative result when the dividend is negative. ```awk -17 % 8 = -1 ``` -------------------------------- ### Create guide.awk for internationalization Source: https://www.gnu.org/software/gawk/manual/html_node/I18N-Example.html The original awk source file using TEXTDOMAIN and the underscore function to mark strings for translation. ```awk BEGIN { TEXTDOMAIN = "guide" bindtextdomain(".") # for testing print _"Don't Panic" print _"The Answer Is", 42 print "Pardon me, Zaphod who?" } ``` -------------------------------- ### Execution of Array Traversal Demonstration Source: https://www.gnu.org/software/gawk/manual/html_node/Controlling-Array-Traversal.html The command to run the demonstration program and the resulting output showing different sorting behaviors. ```bash $ `gawk -f compdemo.awk` -| Sort function: cmp_num_idx _Sort by numeric index_ -| data[two] = 20 -| data[one] = 10 _Both strings are numerically zero_ -| data[10] = one -| data[20] = two -| data[100] = 100 -| -| Sort function: cmp_str_val _Sort by element values as strings_ -| data[one] = 10 -| data[100] = 100 _String 100 is less than string 20_ -| data[two] = 20 -| data[10] = one -| data[20] = two -| -| Sort function: cmp_num_str_val _Sort all numeric values before all strings_ -| data[one] = 10 -| data[two] = 20 -| data[100] = 100 -| data[10] = one -| data[20] = two ``` -------------------------------- ### Self-contained awk script example Source: https://www.gnu.org/software/gawk/manual/html_node/Executable-Scripts.html An example of an awk script file named 'advice' using the #! mechanism to specify the interpreter. ```awk #! /bin/awk -f BEGIN { print "Don't Panic!" } ``` -------------------------------- ### Memory ownership example with char pointers Source: https://www.gnu.org/software/gawk/manual/html_node/API-Ownership-of-MPFR-and-GMP-Values.html An example demonstrating how ownership of memory can be transferred between pointers for standard character arrays. ```c char *p = gawk_malloc(42); p _``owns'' the memory_ char *q = p; p = NULL; _now_ q _``owns'' it_ ``` -------------------------------- ### Example usage of fork() Source: https://www.gnu.org/software/gawk/manual/html_node/Extension-Sample-Fork.html Demonstrates creating a new process and printing different messages based on whether the code is running in the child or parent process. ```awk @load "fork" ... if ((pid = fork()) == 0) print "hello from the child" else print "hello from the parent" ``` -------------------------------- ### Add gawk help to the system library Source: https://www.gnu.org/software/gawk/manual/html_node/OpenVMS-Installation-Details.html Load the gawk help file into the OpenVMS help library to enable command-line help access. ```DCL $ `LIBRARY/HELP sys$help:helplib [.vms]gawk.hlp` ``` -------------------------------- ### Example command for flac-edit Source: https://www.gnu.org/software/gawk/manual/html_node/Shell-Quoting.html An example command that invokes flac-edit with a song title containing special characters, demonstrating the need for shell quoting. ```sh $ `flac-edit -song="Whoope! That's Great" file.flac` ```