__ .---.-.--.--.--.| |--. | _ | | | || < |___._|________||__|__| my learnings and observations of awk == intro == i'm making notes as i audit the awk workshop[1] (run by the author of bottles[2]). == which awk == it looks like on sdf[3] we've all the awks (except the one true awk, although I could be wrong). i think we're using nawk because they have the same version number and usage messages. on my pi (bookworm) i've got nawk and mawk. for demonstration purposes i'm probably going to stick to nawk compatibility. == basics == note: this is mostly cribbed from the pcom chat for day 1[4] - each line is a record - fields in a line are separated by whitespace (including tabs) - code structure (blocks): BEGIN{}, body + functions, END{} - BEGIN{} - initialisation (variables) - {} (body) - aggregation - END{} - summary - scripts can be referenced using `-f` switch and you can keep the blocks in separate scripts - variables get declared using the '-v' switch (see examples) or declare them in the BEGIN{} block - all maths operations are floats - uninitialised variables automagically set to 0 for maths == data transfer (piping, redirection) == ``` # both of these examples have the same behaviour echo 'hello' | awk '//' # // match everything, no filtering awk '//' world.txt # assuming world.txt contains 'hello' ``` == examples ``` # assigning a variable and using command substitution $echo 'AWKward..' \ | awk -vMoo='cowsay' '{print |Moo}END{close(Moo)}' ___________ < AWKward.. > ----------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || # note: you could have used BEGIN{Moo="cowsay"} to avoid using # the '-v' switch # awk as a shebang #! /usr/bin/awk -f BEGIN { print "hello, world" } # shell wrapper script #! /bin/sh - AWKCODE='BEGIN { print "hello, world" }' awk "$AWKCODE" # using body and END{} blocks to count lines seq 0 9 | awk '{Lines = Lines + 1} END{print "Lines = ", Lines}' Lines = 10 # now to sum up numbers seq 0 9 | awk '{Sum = Sum + $1} END{print "Sum = ", Sum}' Sum = 45 == references == == holding pen == links and notes that i haven't referenced in this text file https://rawtext.club/~woog/misc/awk_workshop.html https://git.luxferre.top/awk-gold-collection/log.html == links == [1]: gopher://sdf.org:70/1/users/bottles/awk_workshop/ [2]: gopher://gopher.club:70/1/users/bottles/forum/ [3]: gopher://sdf.org:70/1/sdf/faq/ [4]: gopher://sdf.org:70/0/users/bottles/awk_workshop/Day1.log