Code Snippets Awk

Short texts Awk 🧑‍💻 Code snippets Awk ✍️ Reading lists Awk 👀

anthonywritescode puzzle solution

BEGIN{ tau = 6.2831853 scale = 200 offsetX = 250 offsetY = 250 printf(“\n”) } { num_points = $1 for (i = 0; i < num_points; i++) { x1 = cos(angle) * scale + offsetX y1 = sin(angle) * scale + offsetY angle += tau / num_points for (j = 1; i+j < num_points; j++) […]

Profile YSQL time

#!/usr/bin/awk -f { pid=$2; time=$4; action=$5; # backend state backend_state[0]=”undefined”; backend_state[1]=”idle”; backend_state[2]=”running”; backend_state[3]=”idle in transaction”; backend_state[4]=”fastpath”; backend_state[5]=”idle in transaction aborted”; backend_state[6]=”disabled”; # remove colon from time time=substr(time,1,length(time)-1); # get first relative timestamp in output if ( overall_begin_time == “” ) overall_begin_time = time; # get the first relative timestamp for the pid if ( begin_time[pid] […]

plot.awk

#!/usr/bin/awk -f # This program is a copy of guff, a plot device. https://github.com/silentbicycle/guff # My copy here is written in awk instead of C, has no compelling benefit. # Public domain. @thingskatedid # Run as awk -v x=xyz … or env variables for stuff? # Assumptions: the data is evenly spaced along the x-axis […]

grouper (GNU awk) json output

#!/usr/bin/env gawk -f BEGIN { lfs = length(FS) + 1 } { k = $1 vals[k][vl[k]++] = substr($0, length(k) + lfs) if (!(k in once)) { keys[lk++] = k once[k] } } END { printf “{” asort(keys) for (v in keys) { if (v>1) printf “,” k = keys[v] printf “\”%s\”:[\n”, k asort(vals[k]) for (v […]

hello.awk

// Helper functions // A list consists of any number of ‘rule’, separated by separator, and the last one is optional. const list = (rule, separator) => seq(rule, repeat(seq(separator, rule))) const NL = choice(“\r\n”, “\r”, “\n”) const NLOPT = prec(-1, repeat(NL)) // in ascending order of precedence const operator_table = [ // Name, Prec, symbol, […]