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("[#cyan]Hello World[#-]")  # \033[36mHello World\033[0m


string = bg256(int_colour)

top

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

Example:

# Example for bg256
println bg256(34)  # \033[48;5;34m


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(255,0,0)  # \033[48;2;255;0;0m


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("Hello [#2]%s[#.]","World")  # "Hello World" with World in red ink.


string = clean(string)

top

Remove curly brace nests from a string.

Example:

# Example for clean
println clean("a{b[pad field fields get_value has_start has_end match filter substr gsub replace trim lines count inset wrap next_match line_add line_delete line_replace line_add_before line_add_after line_match line_filter grep sgrep line_head line_tail is_utf8 reverse tr lower upper format ccformat literal pos bg256 fg256 bgrgb fgrgb split join collapse strpos stripansi addansi stripquotes stripcc clean rvalid levdist keys]}d")  # ad


string = collapse(string)

top

Turns a newline separated string into a space separated string.

Example:

# Example for collapse
println collapse("a\nb\nc")  # "a b c"


integer = count(string_name)

top

Returns the number of lines in string_name.

Example:

# Example for count
println count("a\nb\nc")  # 3


string = fg256(int_colour)

top

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

Example:

# Example for fg256
println fg256(31)+"hello"+fg256(0)  # red hello text


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(255,0,0)  # \033[38;2;255;0;0m


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

top

Retrieves columnar field position from input_string.

[#SOL]optional_separator defaults to a space character. string is empty on failure.

Example:

# Example for field
println field("a b c",2)  # "b"


int = fields(input_string[,optional_separator])

top

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

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

[#SOL]Returns -1 on error, or field count.

Example:

# Example for fields
var txt="a b c"
println fields(txt)  # 3
println F  # ["a","b","c"]


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
println filter("abc def","d")  # "def"


string = format(string,var_args)

top

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

Example:

# Example for format
println format("%s %d", "Age:", 30)


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
var m={"key":"val"}
println get_value(m,"key")  # val


nl_string = grep(nl_string,regex)

top

Alias for line_filter.

Example:

# Example for grep
var s="a\nb\nc"
println grep(s,"b")  # "b"


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","x")  # hexxo worxd


bool = has_end(string1,string2)

top

Does string1 end with string2?

Example:

# Example for has_end
println has_end("abcdef","ef")  # true


bool = has_start(string1,string2)

top

Does string1 begin with string2?

Example:

# Example for has_start
println has_start("abc","a")  # true


nl_string = inset(nl_string,distance)

top

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

Example:

# Example for inset
var text="line1\nline2"
println inset(text,4)


string = is_utf8(string)

top

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


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"],",")  # "a,b,c"


[]string = keys(map)

top

Returns a list of map keys


int = levdist(string,string)

top

Calculate Levenshtein edit distance between two strings.


string = line_add(var,string)

top

Append a line to array string var.

Example:

# Example for line_add
var s="a\nb\nc"
println line_add(s,"d")  # "a\nb\nc\nd"


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
var s="a\nb\nc"
println line_add_after(s,"b","inserted")  # a\nb\ninserted\nc


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
var s="a\nb\nc"
println line_add_before(s,"b","x")  # a\nx\nb\nc


string = line_delete(var,regex)

top

Remove lines from array string var which match regex.

Example:

# Example for line_delete
var s="a\nb\nc"
println line_delete(s,"b")  # a\nc


nl_string = line_filter(nl_string,regex)

top

Returns lines from nl_string where regular expression regex matches.

Example:

# Example for line_filter
var s="a\nb\nc"
println line_filter(s,"b")  # "b"


nl_string = line_head(nl_string,count)

top

Returns the top count lines of nl_string.

Example:

# Example for line_head
println line_head("a\nb\nc",2)  # "a\nb"


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
println line_match("a\nb\nc","b")  # true


string = line_replace(var,regex,replacement)

top

Replaces lines in var that match regex with replacement.

Example:

# Example for line_replace
var s="a\nb\nc"
println line_replace(s,"b","x")  # a\nx\nc


nl_string = line_tail(nl_string,count)

top

Returns the last count lines of nl_string.

Example:

# Example for line_tail
println line_tail("a\nb\nc",2)  # "b\nc"


string = lines(string_name,string_range)

top

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

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

Example:

# Example for lines
var s="a\nb\nc"
println lines(s,"1:2")  # "a\nb"


string = literal(string,var_args)

top

Format the input string.


string = lower(string)

top

Convert to lower-case.

Example:

# Example for lower
println lower("HELLO")  # hello


bool = match(string,regex)

top

Does string contain a match for regular expression regex?

Example:

# Example for match
println match("hello","ell")  # true


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
var t="a\nb\nc"
println next_match(t,"b",1)  # 2


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

top

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

Example:

# Example for pad
println pad("abc",1,5,"*")  # "**abc"


string = pos(int_row,int_col)

top

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

Example:

# Example for pos
println pos(10,20)  # ANSI cursor string


string = replace(var,regex,replacement)

top

Replaces matches found in var with regex to replacement.

Example:

# Example for replace
println replace("abc","b","B")  # aBc


list_or_string = reverse(list_or_string)

top

Reverse the contents of a variable.

Example:

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


bool = rvalid(string)

top

Checks if a regex has valid syntax.


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).

[#SOL]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


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

top

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

Example:

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


string = stripansi(string)

top

Remove escaped ansi codes.

Example:

# Example for stripansi
var s=char(27)+"[31mred"+char(27)+"[0m"
println stripansi(s)  # "red"


string = stripcc(string)

top

Remove Za colour codes from string.

Example:

# Example for stripcc
println stripcc("[#red]red[#-]")  # red


string = stripquotes(string)

top

Remove outer quotes (double, single or backtick)

Example:

# Example for stripquotes
println stripquotes("'hello'")  # hello


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("abcde","c")  # 3


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",1,3)  # hel


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("aabbcc","d","ab")  # cc


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("  a  ",0)  # "a"


string = upper(string)

top

Convert to upper-case.

Example:

# Example for upper
println upper("hello")  # HELLO


bool = wrap([bool])

top

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

[#SOL]The previous wrap value is returned.

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