Return to index

string functions

ansi_string = addansi(string)

top

Return a string with za colour codes replaced with ANSI values.

Example:

# Example for addansi
println addansi("[#2]Red Text[#-]")


string = bg256(int_colour)

top

Returns an ANSI code string for expressing an 8-bit background colour code.

Example:

# Example for bg256
println bg256(3.14)


string = bgrgb(int_r,int_g,int_b)

top

Returns an ANSI code string for expressing an rgb background colour code.

Example:

# Example for bgrgb
println bgrgb(3.14, 3.14, 3.14)


string = ccformat(string,var_args)

top

Format the input string in the manner of fprintf(). Also processes embedded colour codes to ANSI.

Example:

# Example for ccformat
println ccformat("[#2]Error[#-]: %s (code: %d)", "File not found", 404)


string = clean(string)

top

Remove curly brace nests from a string.

Example:

# Example for clean
text = "password=secret123&api_key=abc123&token=xyz789"
println clean(text)


string = collapse(string)

top

Turns a newline separated string into a space separated string.

Example:

# Example for collapse
println collapse("hello   world\n\n\nfoo    bar")


integer = count(string_name)

top

Returns the number of lines in string_name.

Example:

# Example for count
text = "line1\nline2\nline3"
println count(text)


string = fg256(int_colour)

top

Returns an ANSI code string for expressing an 8-bit foreground colour code.

Example:

# Example for fg256
println fg256(3.14)


string = fgrgb(int_r,int_g,int_b)

top

Returns an ANSI code string for expressing an rgb foreground colour code.

Example:

# Example for fgrgb
println fgrgb(3.14, 3.14, 3.14)


string = field(input_string,position[,optional_separator])

top

Retrieves columnar field position from input_string.


optional_separator defaults to a space character. string is empty on failure.

Example:

# Example for field
field("hello", 42)  # usage


int = fields(input_string[,optional_separator])

top

Splits up input_string in local array F, with fields starting at index 1.


Field count is stored in NF. Also squeezes repeat spaces when separator is a space char (default).


Returns -1 on error, or field count.

Example:

# Example for fields
fields("hello")  # usage


string = filter(string,regex[,count])

top

Returns a string matching the regular expression regex in string. count should be -1 for all matches.

Example:

# Example for filter
text = "The quick brown fox jumps over the lazy dog"
println filter(text, "[aeiou]+")


string = format(string,var_args)

top

Format the input string in the manner of fprintf().

Example:

# Example for format
println format("Hello %s, you have %d messages", "Alice", 5)


string_value = get_value(string_array,key_name)

top

Returns the value of the key key_name in string_array.

Example:

# Example for get_value
println get_value("name=John\nage=30", "name")


nl_string = grep(nl_string,regex)

top

Alias for line_filter.

Example:

# Example for grep
text = "apple\nbanana\ncherry\napricot"
println grep(text, "^a")


string = gsub(string,string_m,string_s)

top

Returns string with all matches of string_m replaced with string_s.

Example:

# Example for gsub
println gsub("hello world", "l", "L")


bool = has_end(string1,string2)

top

Does string1 end with string2?

Example:

# Example for has_end
println has_end("hello world", "world")


bool = has_start(string1,string2)

top

Does string1 begin with string2?

Example:

# Example for has_start
println has_start("hello world", "hello")


nl_string = inset(nl_string,distance)

top

Left pads each line of nl_string with distance cursor right commands.

Example:

# Example for inset
println inset("hello", 3)
println inset("hello world", 20)


string = is_utf8(string)

top

Checks for utf8 rune and returns width. 1 on no match.

Example:

# Example for is_utf8
println is_utf8("hello")
println is_utf8("\xff\xfe")


string = join([]string_list[,fs])

top

Returns a string with all elements of string_list concatenated, separated by fs.

Example:

# Example for join
println join(["a", "b", "c"])


[]string = keys(map)

top

Returns a list of map keys

Example:

# Example for keys
println keys(map(.a 1, .b 2))


int = levdist(string,string)

top

Calculate Levenshtein edit distance between two strings.

Example:

# Example for levdist
println levdist("kitten", "sitting")
println levdist("saturday", "sunday")


string = line_add(var,string)

top

Append a line to array string var.

Example:

# Example for line_add
text = "apple\nbanana"
println line_add(text, "cherry")


string = line_add_after(var,regex,string)

top

Inserts a new line to array string var after the first matching regex.

Example:

# Example for line_add_after
text = "apple\nbanana\ncherry"
println line_add_after(text, "banana", "blueberry")


string = line_add_before(string,regex_string,string)

top

Inserts a new line in string ahead of the first matching regex_string.

Example:

# Example for line_add_before
text = "apple\nbanana\ncherry"
println line_add_before(text, "banana", "apricot")


string = line_delete(var,regex)

top

Remove lines from array string var which match regex.

Example:

# Example for line_delete
println line_delete("hello\nworld\ntest", "world")


nl_string = line_filter(nl_string,regex)

top

Returns lines from nl_string where regular expression regex matches.

Example:

# Example for line_filter
text = "apple\nbanana\ncherry"
println line_filter(text, "an")


nl_string = line_head(nl_string,count)

top

Returns the top count lines of nl_string.

Example:

# Example for line_head
text = "line1\nline2\nline3"
println line_head(text, 2)


bool = line_match(nl_string,regex)

top

Does nl_string contain a match for regular expression regex on any line?

Example:

# Example for line_match
text = "apple\nbanana\ncherry"
println line_match(text, "an")


string = line_replace(var,regex,replacement)

top

Replaces lines in var that match regex with replacement.

Example:

# Example for line_replace
text = "apple\nbanana\ncherry"
println line_replace(text, "banana", "blueberry")


nl_string = line_tail(nl_string,count)

top

Returns the last count lines of nl_string.

Example:

# Example for line_tail
text = "line1\nline2\nline3"
println line_tail(text, 2)


string = lines(string_name,string_range)

top

Returns lines from string_name. string_range is specified in the form start:end.


Either optional term can be last to indicate the last line of the file. Numbering starts from 0.

Example:

# Example for lines
text = "line1\nline2\nline3"
println lines(text, "1:2")


string = literal(string,var_args)

top

Format the input string.

Example:

# Example for literal
literal("Hello %s, you have %d messages\n", "Alice", 5)


string = lower(string)

top

Convert to lower-case.

Example:

# Example for lower
println lower("Hello World")


bool = match(string,regex)

top

Does string contain a match for regular expression regex?

Example:

# Example for match
println match("hello world", "world")


int = next_match(nl_string,regex,start_line)

top

Returns the next line number which contains the regex in nl_string. -1 is returned on no match.

Example:

# Example for next_match
println next_match("hello world", "l", 0)
println next_match("hello world", "l", 3)


string = pad(string,justify,width[,padchar])

top

Return left (-1), centred (0) or right (1) justified, padded string.

Example:

# Example for pad
println pad("hello", 0, 10)
println pad("hello", -1, 10, "-")
println pad("hello", 1, 10, "-")


string = pos(int_row,int_col)

top

Returns a cursor positioning ANSI code string for (row,col).

Example:

# Example for pos
print pos(1, 1)
println "Cursor repositioned"


string = progress_bar(percentage[,width[,fill[,empty]]])

top

Returns a progress bar string.

Example:

# Example for progress_bar
try
    progress_bar(50, 100)
catch err
    println "Error"
endtry


string = replace(var,regex,replacement)

top

Replaces matches found in var with regex to replacement.

Example:

# Example for replace
println replace("hello world", "world", "everyone")


list_or_string = reverse(list_or_string)

top

Reverse the contents of a variable.

Example:

# Example for reverse
println reverse([1, 2, 3, 4, 5])


bool = rvalid(string)

top

Checks if a regex has valid syntax.

Example:

# Example for rvalid
println rvalid("^[a-z]+$")
println rvalid("[invalid")


array_of_maps = sgrep(filename,options_map)

top

Search file for regex pattern with context. Options: .regex (required), .before (int), .after (int), .number (bool), .only_matching (bool).


Returns array of maps with line data and context. (map fields: .line,.linenum,.context_before_count,.context_after_count,.context_before[],.context_after[],.text[] when .only_matching=true

Example:

# Example for sgrep
write_file("/tmp/za_sgrep.txt", "hello
world")
sgrep("/tmp/za_sgrep.txt", map(.regex "hello"))


[]list = split(string[,fs])

top

Returns string as a list, breaking the string on fs.

Example:

# Example for split
println split("a,b,c", ",")


string = stripansi(string)

top

Remove escaped ansi codes.

Example:

# Example for stripansi
text = addansi("[#2]Red Text[#-]")
println stripansi(text)


string = stripcc(string)

top

Remove Za colour codes from string.

Example:

# Example for stripcc
println stripcc("[#2]Red Text[#-] and [#4]Green Text[#-]")


string = stripquotes(string)

top

Remove outer quotes (double, single or backtick)

Example:

# Example for stripquotes
println stripquotes('"hello world"')
println stripquotes("'single quoted'")


int_position = strpos(string,substring[,start_pos])

top

Returns the position of the next match of substring in string. Returns -1 if no match found.

Example:

# Example for strpos
println strpos("hello world", "world")


string = substr(string,int_s,int_l)

top

Returns a sub-string of string, from position int_s with length int_l.

Example:

# Example for substr
println substr("hello world", 0, 5)


string = tr(string,action,case_string[,translation_string])

top

delete (action "d") or squeeze (action "s") extra characters (in case_string) from string.

Translate (action "t") can be used, along with the optional translation_string to specify direct

replacements for existing characters. Please note: this is a very restricted subset of the tr tool.

Example:

# Example for tr
println tr("hello world", "lo", "12")


string = trim(string,int_type[,removal_list_string])

top

Removes whitespace from string, depending on int_type.

-1 ltrim, 0 both, 1 rtrim. By default, space (ASCII:32) and horizontal tabs (ASCII:9) are removed.

Example:

# Example for trim
println trim("  hello world  ", 0)
println trim("  hello world  ", -1)
println trim("  hello world  ", 1)
println trim("--hello world--", 0, "-")


string = trunc(string,width[,ellipsis])

top

Truncates string to width runes, appending ellipsis if truncated.

Example:

# Example for trunc
println trunc("The quick brown fox jumps over the lazy dog", 20)
println trunc("hello world", 5, "..")


string = upper(string)

top

Convert to upper-case.

Example:

# Example for upper
println upper("Hello World")


[]any = values(map)

top

Returns a list of map values

Example:

# Example for values
println values(map(.a 1, .b 2))


bool = wrap([bool])

top

Enable (true) or disable (false) line wrap in non-global display panes.


The previous wrap value is returned.


N.B. ensure that only plain text is wrapped. ANSI codes may be split!

Example:

# Example for wrap
wrap()  # usage


string = wrap_text(string [, number])

top

Wrap text at console width (default) or specified width, preserving words and ANSI codes.

Example:

# Example for wrap_text
text = "The quick brown fox jumps over the lazy dog."
println wrap_text(text, 20)