Awk cheatsheet
From Lolly's Wiki
Functions
Bytes to human readable
function b2h(value){
# Bytes to human readable
unit=1;
while(value>=1024){
unit++;
value/=1024;
}
split("B,KB,MB,GB,TB,PB", unit_string, /,/);
return sprintf("%.2f%s",value,unit_string[unit]);
}
Binary to decimal
function b2d(bin){
len=length(bin);
for(i=1;i<=len;i++){
dec+=substr(bin,i,1)*2^(len-i);
}
return dec;
}
Quicksort
This is not my code! It is taken from [here]. You can call it like this: qsort(array,1,length(array));
# BEGIN http://awk.info/?quicksort
function qsort(A, left, right, i, last) {
if (left >= right)
return
swap(A, left, left+int((right-left+1)*rand()))
last = left
for (i = left+1; i <= right; i++)
if (int(A[i]) < int(A[left]))
swap(A, ++last, i)
swap(A, left, last)
qsort(A, left, last-1)
qsort(A, last+1, right)
}
# Helper function to swap two elements of an array
function swap(A, i, j, t) {
t = A[i]; A[i] = A[j]; A[j] = t
}
# END http://awk.info/?quicksort
Sort words inside braces (gawk)
Written for beautifying lines like
GRANT SELECT (account_id, user, enable_imap, fk_domain_id, enable_virusscan, max_msg_size, from_authuser_only, id, time_start, changed_by, enable_spamblocker, onhold, time_end, archive, mailbox_quota) ON `mail_db`.`mail_account` TO 'user'@'172.16.16.16'
This function:
function inner_brace_sort (rest, delimiter) {
sorted="";
while( match(rest,/\([^\)]+\)/) ) {
sorted=sprintf("%s%s", sorted, substr(rest, 1, RSTART));
inner=substr(rest, RSTART+1, RLENGTH-2);
rest=substr(rest, RSTART+RLENGTH-1, length(rest));
split(inner, inner_a, delimiter);
inner_l=asort(inner_a, inner_s);
for(i=1; i<=inner_l; i++) {
sorted=sprintf("%s%s", sorted, inner_s[i]);
if(i<inner_l) sorted=sprintf("%s, ", sorted);
}
sorted=sprintf("%s", sorted);
}
return sorted""rest;
}
Sorts the fields inside the braces alphabetically and can be called like this:
/\(/ {
print inner_brace_sort($0, ",[ ]*");
}