💾 Archived View for gemini.susa.net › awk_notes.gmi captured on 2022-04-29 at 11:23:25. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

-=-=-=-=-=-=-

Awk Cheat Sheet of sorts

Awk User's Guide

Regex

IGNORECASE=1

string ~ /pattern/

string !~ /pattern/

^, $ begin and end of Line

\A, \z begin end end of Word

[^0-9] non-digit

\w \s \d word, whitespace, digit

\W \S \D non-(word, whitespace, digit)

[:alnum:] [:alpha:] [:space:] [:upper:] [:lower:] [:punct:]

[:digit:] [:xdigit:] (hexadecimals)

Quantifiers: . + * ? {n} {n,} {n,m}

Groups: (something) (^word | word$) (\w+ ){3}

String functions

sub(regexp, replacement [, target])

gsub(regexp, replacement [, target])

split(string, array [, fieldsep [, seps ] ]) ( e.g. "," )

patsplit(string, array [, fieldpat [, seps ] ]) ( e.g. "(\d+)-(\w+)" )

sub and split return number of substitutions or split-items

substr(string, start [, length ])

index(haystack, needle)

1-based indexes are returned

toupper() & tolower() return a new modified string.

Control Statements

for (idx=0; idx < 5; idx++) { }

for (idx in my_array) { } # See PROCINFO["sorted_in"]

if (condition) { } else { }

while (condition) { }

do { } while (condition)

switch (a_name) {

case "Kevin":

print "Hello"

break

case /Santa( clause)?/

print "Who told you I've been bad?"

break

default:

print "Please to meet you"

}

break, continue, next, nextfile, exit [return code]

Variables

FS field separator, char or regex. Value of " " means 'any whitespace'.

OFS output separator for print $1, $2, $3

ORS output record separator ("\n")

ARGC, ARGV - argv is 0-indexed array with command name as ARGV[0]

ENVIRON - array of env vars, keyed on the variable's name. ENVIRON["PATH"]

FILENAME - the current filename. In BEGIN, use getline to force a value.

FNR - line number within the current file.

NR - line number since awk was invoked.

NF - number of fields on the current line.

PROCINFO - internal information, too much to write about.

Functions

function myprint(num)
{
    printf "%6.3g\n", num
    return "OK"
}

Other notes

BEGIN, END, BEGINFILE, ENDFILE - special meta-patterns

-v 'myvar=something' - define a variable using a command line parameter

Sort order when fetching 'for (subscript in my_array)'

PROCINFO["sorted_in"] = "@ind_num_desc", or "@val_str_asc", or "@undefined"