This document lists all of the builtin functions and commands supported by
frawk. For those interested in a source of truth on these components, check out
the "builtins" module in
src/builtins.rs
.
Unlike Awk, builtin functions must have parentheses directly following the
function name. Awk supports C-style syntax like length (s)
, but only with
builtin functions: user-defined functions must still be called like foo(x)
. In
frawk, builtin and user-defined functions are called with the same syntax: with
no spaces allowed.
Binary operators:
- Arithmetic:
+
,-
,/
,*
,*
,^
(which is exponentiation), and%
- Comparison (which also work on strings):
<
,>
,<=
,>=
,==
,!=
.
Unary Operators:
$x
: Get columnx
.+
,-
: Unary "positive" and negation.!
: logical negation.
- Floating-point operations:
sin
,cos
,atan
,atan2
,log
,log2
,log10
,sqrt
,exp
are delegated to the Rust standard library, or LLVM intrinsics where available. rand()
: Returns a uniform random floating-point number between 0 and 1.srand(x)
: Seeds the random number generator used byrand
, returns the old seed.- Bitwise operations. All of these operations coerce their operands to integers
before being evaluated.
compl(x)
: Bitwise complement.and(x, y)
: Bitwise and.or(x, y)
: Bitwise or.xor(x, y)
: Bitwise xor.lshift(x, y)
: Shiftx
left byy
bits.rshift(x, y)
: Arithmetic right shift ofx
byy
bits.rshiftl(x, y)
: Logical right shift ofx
byy
bits.
s ~ re
: 1 if strings
matches regular expression inre
.s !~ re
: Equivalent to negating the result ofs ~ re
.match(s, re)
: 1 if strings
matches the regular expression inre
. Ifs
matches, theRSTART
variable is set with the start of the leftmost match ofre
, andRLENGTH
is set with the length of this match.substr(s, i[, j])
: The 1-indexed substring of strings
starting from indexi
and continuing for the nextj
characters or until the end ofs
ifi+j
exceeds the length ofs
or ifs
is not provided.sub(re, t, s)
: Substitutest
for the first matching occurrence of regular expressionre
in the strings
.gsub(re, t, s)
: Likesub
, but with all occurrences substituted, not just the first.index(haystack, needle)
: The first index withinhaystack
in which the stringneedle
occurs, 0 ifneedle
does not appear.split(s, m[, fs])
: Splits the strings
according tofs
, placing the results in the arraym
. Iffs
is not specified then theFS
variable is used to splits
.sprintf(fmt, s, ...)
: Returns a string formatted according tofmt
and provided arguments. The goal is to provide the semantics of the libcsprintf
function.print(s, ...) [>[>] out]
: Print the argumentss
separated byOFS
. If>> out
is provided then the output is appended to the fileout
, if> out
is provided then any data inout
is overwritten. Parentheses are optional inprint
, but parsing of non-parenthesized arguments proceeds differently to avoid potential ambiguities.printf(fmt, s, ...) [>[>] out]
: Likesprintf
but the result of the operation is written to standard output, or toout
according to the append or overwrite semantics specified by>
or>>
. Likeprint
,printf
can be called without parentheses around its arguments, though arguments are parsed differently in this mode to avoid ambiguities.hex(s)
: Returns the hexadecimal integer (e.g.0x123abc
) encoded ins
, or0
otherwise.join_fields(i, j[, sep])
: Returns columnsi
throughj
(1-indexed, inclusive) concatenated together, joined bysep
, or byOFS
ifsep
is not provided.escape_csv(s)
: Returnss
escaped as a CSV column, adding quotes if necessary, replacing quotes with double-quotes, and escaping other whitespace.escape_tsv(s)
: Returnss
escaped as a TSV column. There is less to do with CSV, but tab and newline characters are replaced with\t
and\n
.join_csv(i, j)
: Likejoin_fields
but with columns joined by,
and escaped usingescape_csv
.join_tsv(i, j)
: Likejoin_fields
but with columns joined by tabs and escaped usingescape_tsv
.int(s)
: Converts
to an integer. Floating-point numbers are also converted (rounded down), potentially without a round-trip through a string representation.tolower(s)
: Returns a copy ofs
where all uppercase ASCII characters are replaced with their lowercase counterparts; other characters are unchanged.toupper(s)
: Returns a copy ofs
where all lowercase ASCII characters are replaced with their uppercase counterparts; other characters are unchanged.exit [code]
: Exits the current process with the given code.exit
attempts to flush any open file buffers. For parallel scripts, other worker threads have inputs cut off. Once those threads exit their main loop the process exits with the given exit code. This means that scripts with long loop iterations may not exit immediately.exit
can be called with and without parentheses.
close(s)
flushes all pending output to files
and then closes it.length(x)
returns the length ofx
, wherex
can be either a string or an array.system(s)
runs the command contained in the strings
in a subshell, returning the error code, or the integer1
if an error code was unavailable. The strings
is subject to taint analysis by default.