This repository contains various helper functions for awk.
To use them, either include whole thing in your program or run awk like below. Every function in this repository is POSIX-compatible.
awk -f awk++ -f myscript.awk /etc/passwd
Functions:
-
strtonum(str, base)
- convertsstring
frombase
X to base 10 - just like gawk specific option, except with base argument. If base isn't specified, heurestics are used to detect it (if it starts with0x
then it isbase
16, if it starts with0
then it isbase
8,base
10 (useless) otherwise. -
numtostr(number, base)
- exact reverse ofstrtonum
, it converts base 10number
to otherbase
. -
ceil(number)
- ceilsnumber
. Note that there is nofloor
function (int
works exactly like it). -
ord(character)
- convertscharacter
to number. As this information isn't publicly available in awk, when this function will be used for first time, the table of characters will be created. -
chr(number)
- converts character to itsnumber
, just likeord()
except reverse. -
reverse(string)
- reversesstring
. -
len(array)
- gets length ofarray
. -
empty(array)
- emptiesarray
(just likedelete
in gawk, except it's not implementation-specific). -
push(array, value)
- pushesvalue
toarray
. -
pop(array)
- popsvalue
fromarray
. -
shift(array)
- shiftsvalue
fromarray
. -
unshift(array, value)
- unshiftsvalue
fromarray
. -
clone(return, array)
- clonesarray
. -
join(array, separator)
- joinsarray
byseparator
. -
slice(return, array, start, end)
- slicesarray
,start
is first array element,end
is last array element. Negative values mean counting from end. -
first(array)
- returns firstarray
element (identical toarray[1]
). -
first(return, array, n)
- returns firstn
array
elements. -
initial(return, array, n)
- doesn't return lastn
elements. -
last(array)
- returns lastarray
element -
last(return, array, n)
- returns lastn
array
elements. -
rest(return, array, n)
- doesn't return firstn
elements. -
compact(return, array)
- returns array without falsy values. -
contains(array, value)
- returns first position ofvalue
. -
difference(return, array, without)
-array
difference. -
intersection(return, array, with)
- values in botharray
andwith
arrays. -
union(ret, array1, array2)
- join two arrays. -
unique(ret, array)
- returns uniquearray
elements. -
flip(ret, array)
- reversesarray
.