Name

CUT — Cuts a string based on a given delimiter

Synopsis

CUT(varname,char-delim,range-s)

CUT() works in a similar manner to the cut(1) Unix command-line tool, and is, in fact, designed based upon that tool.

In the dialplan, you may specify character offsets to select a substring of a variable based purely on the uniform length of characters (namely 1). CUT() is designed to help you work with data that may have multiple, variable-length sections, divided by a common delimiter.

The most common case is the name of a channel, which is composed of two parts, a base name and a unique identifier (e.g., SIP/tom-abcd1234 or SIP/bert-1a2b3c4d). CUT() may be used to trim the unique identifier, no matter how long the base name may be:

; Trim the unique identifier from the current channel name
exten => 123,1,Set(chan=${CUT(CHANNEL,-,1)})

varname is the name of the variable that will be operated on. Note that CUT() operates on the name of a variable, rather than upon the value of a variable. CUT() is somewhat unique in this regard.

char-delim is the character that will serve as the delimiter ('-' is the default)

range-spec allows you to define which fields are returned. The range-spec can be specified as a range with - (e.g., 1-3) or a group of ranges and field numbers by separating each with & (e.g., 1&3-4). Note that if multiple field numbers are specified, the resulting value will have its fields separated by the same delimiter.

Tip

range-spec uses a 1-based offset. That is, the first field is field 1 (as opposed to a 0-based offset, where the first field would be 0).

See Also

FIELDQTY()

Get Asterisk: The Future of Telephony, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.