Awk cheatsheet: Difference between revisions

From Lolly's Wiki
Jump to navigationJump to search
m (Text replacement - "[[Kategorie:" to "[[Category:")
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Kategorie:AWK|Cheatsheet]]
[[Category:AWK|Cheatsheet]]


==Functions==
==Functions==
===Bytes to human readable===
===Bytes to human readable===
<source lang=awk>
<syntaxhighlight lang=awk>
function b2h(value){
function b2h(value){
         # Bytes to human readable
         # Bytes to human readable
Line 14: Line 14:
         return sprintf("%.2f%s",value,unit_string[unit]);
         return sprintf("%.2f%s",value,unit_string[unit]);
}
}
</source>
</syntaxhighlight>


===Binary to decimal===
===Binary to decimal===
<source lang=awk>
<syntaxhighlight lang=awk>
function b2d(bin){
function b2d(bin){
   len=length(bin);
   len=length(bin);
Line 25: Line 25:
   return dec;
   return dec;
}
}
</source>
</syntaxhighlight>


===Quicksort===
===Quicksort===
This is not my code! It is taken from [[http://awk.info/?quicksort here]] maybe slightly modified cannot check, site is down.
This is not my code! It is taken from [[http://awk.info/?quicksort here]] maybe slightly modified cannot check, site is down.
You can call it like this: qsort(array,1,length(array));
You can call it like this: qsort(array,1,length(array));
<source lang=awk>
<syntaxhighlight lang=awk>
# BEGIN http://awk.info/?quicksort
# BEGIN http://awk.info/?quicksort
function qsort(A, left, right,  i, last) {
function qsort(A, left, right,  i, last) {
Line 49: Line 49:
}
}
# END http://awk.info/?quicksort
# END http://awk.info/?quicksort
</source>
</syntaxhighlight>


Same for alphanumeric:
Same for alphanumeric:
<source lang=awk>
<syntaxhighlight lang=awk>
# BEGIN http://awk.info/?quicksort
# BEGIN http://awk.info/?quicksort
function qsort(A, left, right,  i, last) {
function qsort(A, left, right,  i, last) {
Line 71: Line 71:
}
}
# END http://awk.info/?quicksort
# END http://awk.info/?quicksort
</source>
</syntaxhighlight>


Test:
Test:
<source lang=awk>
<syntaxhighlight lang=awk>
BEGIN {
BEGIN {
   string="1524097359810345254";
   string="1524097359810345254";
Line 127: Line 127:
}
}


</source>
</syntaxhighlight>
which outputs:
which outputs:
<pre>
<pre>
Line 137: Line 137:


Written for beautifying lines like
Written for beautifying lines like
<source lang=mysql>
<syntaxhighlight lang=mysql>
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'
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'
</source>
</syntaxhighlight>


This function:
This function:
<source lang=awk>
<syntaxhighlight lang=awk>
function inner_brace_sort (rest, delimiter) {
function inner_brace_sort (rest, delimiter) {
   sorted="";
   sorted="";
Line 159: Line 159:
   return sorted""rest;
   return sorted""rest;
}
}
</source>
</syntaxhighlight>
Sorts the fields inside the braces alphabetically and can be called like this:
Sorts the fields inside the braces alphabetically and can be called like this:
<source lang=awk>
<syntaxhighlight lang=awk>
/\(/ {
/\(/ {
   print inner_brace_sort($0, ",[ ]*");
   print inner_brace_sort($0, ",[ ]*");
}
}
</source>
</syntaxhighlight>

Latest revision as of 00:00, 26 November 2021


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] maybe slightly modified cannot check, site is down. 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

Same for alphanumeric:

# 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

Test:

BEGIN {
  string="1524097359810345254";
  split(string,array_i,"");
  string="ThisIsAQsortExample";
  split(string,array_a,"");
}
# BEGIN http://awk.info/?quicksort
function qsort_i(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_i(A, left, last-1)
  qsort_i(A, last+1, right)
}
function qsort_a(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 (A[i] < A[left])
      swap(A, ++last, i)
  swap(A, left, last)
  qsort_a(A, left, last-1)
  qsort_a(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 {
  for(element in array_i)
    printf array_i[element];
  printf " ===qsort==> "
  qsort_i(array_i,1,length(array_i));
  for(element in array_i)
    printf array_i[element];
  print;
  for(element in array_a)
    printf array_a[element];
  printf " ===qsort==> "
  qsort_a(array_a,1,length(array_a));
  for(element in array_a)
    printf array_a[element];
  print;
}

which outputs:

1524097359810345254 ===qsort==> 0011223344455557899
ThisIsAQsortExample ===qsort==> AEIQTaehilmoprssstx

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, ",[ ]*");
}