The text:trim() function removes whitespace
from the beginning and end of strings (leading and trailing
whitespace characters), supports all Unicode whitespace
characters following the
Unicode
Character Database, Unicode Standard Annex #44, and
processes text based on Unicode logical order, not display
order.
| Parameter | Type | Required | Default Value | Description |
|---|---|---|---|---|
as | string | optional[a] | _trim | The name for the output string. |
field[b] | expression | required | The input string to trim. | |
mode | string | optional[a] | both | Where to remove whitespace: start, end, or both. |
| Values | ||||
both | Remove whitespace from both ends of the string | |||
end | Remove whitespace from the end of the string | |||
start | Remove whitespace from the start of the string | |||
[a] Optional parameters use their default value unless explicitly set. | ||||
Hide omitted argument names for this function
Omitted Argument NamesThe argument name for
fieldcan be omitted; the following forms of this function are equivalent:logscale Syntaxtext:trim("value")and:
logscale Syntaxtext:trim(field="value")These examples show basic structure only.
text:trim() Function Operation
The text:trim() function has specific
implementation and operational considerations, outlined below.
Core Functionality
Removes whitespace from the beginning and end of strings.
Supports all Unicode whitespace characters as defined in the Unicode standard, going beyond ASCII whitespace characters.
Processes text based on Unicode logical order, not display order.
Unicode Handling
Removes all Unicode-defined whitespace characters, extending beyond the basic ASCII whitespace characters (space, horizontal and vertical tab, line feed, form feed, and carriage return).
Covers a broader range than LogScale's regular expression
\sclass, instead matching the combined patterns of\h(Unicode horizontal whitespace) and\v(Unicode vertical whitespace).For left-to-right scripts like English, the logical order matches the display order. For example,
text:trim(" cat ", mode=end)produces" cat".For right-to-left scripts (Arabic, Hebrew), the logical ending is displayed on the left when the input string is exclusively so. For example,
text:trim(" كتاب ", mode=end)may be displayed as"كتاب ".
Trimming Behavior
Trimming behavior is controlled by the Unicode
White_space property, as defined in the
Unicode
Character Database, Unicode Standard Annex #44:
At string start: Stops at the first character without
White_space=yes.At string end: Starts after the last character without
White_space=yes.Preserves whitespace in two locations: after bidirectional Unicode markers at the beginning of text, and before bidirectional Unicode markers at the end of text. Bidirectional Unicode markers are special characters that control text direction in languages that can be written left-to-right or right-to-left.
Note
For right-to-left scripts like Arabic and Hebrew, the trimming follows Unicode logical order rather than visual display order. This means the function processes text according to the underlying Unicode representation, which may differ from the visual appearance of the text.
text:trim() Syntax Examples
This example demonstrates basic whitespace removal from both ends of a string.
text:trim(" abc ")
If input data was " abc ", it would return
"abc" .
This example demonstrates removing whitespace only from the start of a string.
text:trim(" abc ", mode=start)
If input data was " abc ", it would return
"abc " .
This example demonstrates removing whitespace only from the end of a string.
text:trim(" abc ", mode=end)
If input data was " abc ", it would return
" abc".
This example shows how the function handles Unicode characters.
text:trim(" 😀 नमसते ")
If input data was " 😀 नमसते ",
it would return "😀 नमसते".
text:trim() Examples
Click next to an example below to get the full details.
Remove Both Leading And Trailing Whitespace From Log Messages
Clean up log messages using text:trim()
function
Query
text:trim(message, as="cleaned_message")Introduction
In this example, the text:trim() function is used
to clean up log messages that contain unnecessary whitespace at both
ends, which commonly occurs in log files with inconsistent formatting or
when messages are padded for alignment.
Example incoming data might look like this:
"@timestamp","message","severity"
"2025-11-05T10:00:00Z"," Server status: OK ","INFO"
"2025-11-05T10:00:01Z"," Memory usage at 85% ","WARN"
"2025-11-05T10:00:02Z"," Disk space critical ","ERROR"
"2025-11-05T10:00:03Z"," Backup process started ","INFO"
"2025-11-05T10:00:04Z"," Connection pool: 50/100 ","DEBUG"
"2025-11-05T10:00:05Z"," Service health check completed ","INFO"Step-by-Step
Starting with the source repository events.
- logscale
text:trim(message, as="cleaned_message")Creates a field named cleaned_message that contains the message text with both leading and trailing whitespace removed. The
text:trim()function removes all whitespace characters from both ends of the string while preserving any whitespace between words. If nomodeparameter is specified, it defaults toboth. Theasparameter specifies the output field name. Event Result set.
Summary and Results
The query is used to standardize log messages by removing all unnecessary whitespace from both ends of the messages, creating cleaner and more consistent log entries.
This query is useful, for example, to normalize log messages for pattern matching, improve readability, or prepare data for exports where consistent formatting is required.
Sample output from the incoming example data:
"cleaned_message","message","severity"
"Server status: OK"," Server status: OK ","INFO"
"Memory usage at 85%"," Memory usage at 85% ","WARN"
"Disk space critical"," Disk space critical ","ERROR"
"Backup process started"," Backup process started ","INFO"
"Connection pool: 50/100"," Connection pool: 50/100 ","DEBUG"
"Service health check completed"," Service health check completed ","INFO"Note that internal spaces between words are preserved, only the leading and trailing whitespace is removed.
Remove Leading Whitespace From Log Messages
Clean up log messages using the text:trim()
function
Query
text:trim(message, mode=start, as="cleaned_message")Introduction
In this example, the text:trim() function is used
to clean up log messages that contain unnecessary leading whitespace
characters, making the data more consistent and easier to read.
Example incoming data might look like this:
"@timestamp","level","message"
"2025-11-05T10:00:00Z","INFO"," Starting application server"
"2025-11-05T10:00:01Z","INFO"," Database connection established"
"2025-11-05T10:00:02Z","WARN"," Invalid user credentials"
"2025-11-05T10:00:03Z","ERROR"," Authentication service unavailable"
"2025-11-05T10:00:04Z","INFO"," User session created"Step-by-Step
Starting with the source repository events.
- logscale
text:trim(message, mode=start, as="cleaned_message")Creates a field named cleaned_message that contains the message text with leading whitespace removed. The
text:trim()function withmode=startonly removes whitespace characters from the beginning of the string, preserving any trailing spaces if they exist. Theasparameter specifies the output field name. Event Result set.
Summary and Results
The query is used to standardize log messages by removing unnecessary leading whitespace that might have been introduced during logging.
This query is useful, for example, to improve log message readability and consistency, especially when dealing with logs from multiple sources that might format their messages differently.
Sample output from the incoming example data:
"cleaned_message","level","message"
"Starting application server","INFO"," Starting application server"
"Database connection established","INFO"," Database connection established"
"Invalid user credentials","WARN"," Invalid user credentials"
"Authentication service unavailable","ERROR"," Authentication service unavailable"
"User session created","INFO"," User session created"Note that internal spaces between words are preserved, only the leading whitespace is removed.
Remove Trailing Whitespace From Log Messages
Clean up log messages using the text:trim()
function
Query
text:trim(message, mode=end, as="cleaned_message")Introduction
In this example, the text:trim() function is used
to clean up log messages that contain unnecessary trailing whitespace
characters, which often occur when log messages are padded to a fixed
width or when concatenating strings.
Example incoming data might look like this:
"@timestamp","level","message"
"2025-11-05T10:00:00Z","INFO","User login successful "
"2025-11-05T10:00:01Z","WARN","Failed password attempt "
"2025-11-05T10:00:02Z","INFO","Session timeout "
"2025-11-05T10:00:03Z","ERROR","Database connection lost "
"2025-11-05T10:00:04Z","INFO","Cache cleared "
"2025-11-05T10:00:05Z","DEBUG","Memory usage: 85% "Step-by-Step
Starting with the source repository events.
- logscale
text:trim(message, mode=end, as="cleaned_message")Creates a field named cleaned_message that contains the message text with trailing whitespace removed. The
text:trim()function withmode=endonly removes whitespace characters from the end of the string, preserving any leading spaces if they exist. Theasparameter specifies the output field name. Event Result set.
Summary and Results
The query is used to standardize log messages by removing unnecessary trailing whitespace.
This query is useful, for example, to clean up fixed-width formatted logs, improve data consistency for downstream processing, or prepare messages for exact string matching operations where trailing spaces might cause false negatives.
Sample output from the incoming example data:
"cleaned_message","level","message"
"User login successful","INFO","User login successful "
"Failed password attempt","WARN","Failed password attempt "
"Session timeout","INFO","Session timeout "
"Database connection lost","ERROR","Database connection lost "
"Cache cleared","INFO","Cache cleared "
"Memory usage: 85%","DEBUG","Memory usage: 85% "Note that internal spaces between words are preserved, only the trailing whitespace is removed.
This is particularly useful when working with fixed-width formatted logs where trailing spaces are used for padding but are not meaningful for analysis.