frawk Builtin Functions and Commands
October 15, 2021 ยท View on GitHub
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.
Operators
Binary operators:
- Arithmetic:
+,-,/,*,*,^(which is exponentiation), and% - Comparison (which also work on strings):
<,>,<=,>=,==,!=.
Unary Operators:
$x: Get columnx.+,-: Unary "positive" and negation.!: logical negation.
Math
- Floating-point operations:
sin,cos,atan,atan2,log,log2,log10,sqrt,expare 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): Shiftxleft byybits.rshift(x, y): Arithmetic right shift ofxbyybits.rshiftl(x, y): Logical right shift ofxbyybits.
String Operations
s ~ re: 1 if stringsmatches regular expression inre.s !~ re: Equivalent to negating the result ofs ~ re.match(s, re): 1 if stringsmatches the regular expression inre. Ifsmatches, theRSTARTvariable is set with the start of the leftmost match ofre, andRLENGTHis set with the length of this match.substr(s, i[, j]): The 1-indexed substring of stringsstarting from indexiand continuing for the nextjcharacters or until the end ofsifi+jexceeds the length ofsor ifsis not provided.sub(re, t, s): Substitutestfor the first matching occurrence of regular expressionrein the strings.gsub(re, t, s): Likesub, but with all occurrences substituted, not just the first.index(haystack, needle): The first index withinhaystackin which the stringneedleoccurs, 0 ifneedledoes not appear.split(s, m[, fs]): Splits the stringsaccording tofs, placing the results in the arraym. Iffsis not specified then theFSvariable is used to splits.sprintf(fmt, s, ...): Returns a string formatted according tofmtand provided arguments. The goal is to provide the semantics of the libcsprintffunction.print(s, ...) [>[>] out]: Print the argumentssseparated byOFS. If>> outis provided then the output is appended to the fileout, if> outis provided then any data inoutis overwritten. Parentheses are optional inprint, but parsing of non-parenthesized arguments proceeds differently to avoid potential ambiguities.printf(fmt, s, ...) [>[>] out]: Likesprintfbut the result of the operation is written to standard output, or tooutaccording to the append or overwrite semantics specified by>or>>. Likeprint,printfcan 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, or0otherwise.join_fields(i, j[, sep]): Returns columnsithroughj(1-indexed, inclusive) concatenated together, joined bysep, or byOFSifsepis not provided.escape_csv(s): Returnssescaped as a CSV column, adding quotes if necessary, replacing quotes with double-quotes, and escaping other whitespace.escape_tsv(s): Returnssescaped as a TSV column. There is less to do with CSV, but tab and newline characters are replaced with\tand\n.join_csv(i, j): Likejoin_fieldsbut with columns joined by,and escaped usingescape_csv.join_tsv(i, j): Likejoin_fieldsbut with columns joined by tabs and escaped usingescape_tsv.int(s): Convertsto an integer. Floating-point numbers are also converted (rounded down), potentially without a round-trip through a string representation.tolower(s): Returns a copy ofswhere all uppercase ASCII characters are replaced with their lowercase counterparts; other characters are unchanged.toupper(s): Returns a copy ofswhere all lowercase ASCII characters are replaced with their uppercase counterparts; other characters are unchanged.exit [code]: Exits the current process with the given code.exitattempts 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.exitcan be called with and without parentheses.
Other Functions
close(s)flushes all pending output to filesand then closes it.length(x)returns the length ofx, wherexcan be either a string or an array.system(s)runs the command contained in the stringsin a subshell, returning the error code, or the integer1if an error code was unavailable. The stringsis subject to taint analysis by default.