Trim Last
Synopsis
A text processing processor that removes a specified number of characters or predefined keywords from the end of strings, providing precise control over suffix removal for data cleaning and normalization tasks.
Schema
- trim_last:
field: <ident>
count: <integer>
keywords: <string[]>
target_field: <ident>
description: <text>
if: <script>
ignore_failure: <boolean>
ignore_missing: <boolean>
on_failure: <processor[]>
on_success: <processor[]>
tag: <string>
Configuration
The following fields are used to define the processor:
| Field | Required | Default | Description |
|---|---|---|---|
field | Y | - | Field containing the string(s) to process |
count | N | - | Number of characters to remove from end |
keywords | N | - | Keywords to remove from end |
target_field | N | field | Field to store the trimmed result |
description | N | - | Explanatory note |
if | N | - | Condition to run |
ignore_failure | N | false | Continue if trimming fails |
ignore_missing | N | false | Continue if source field doesn't exist |
on_failure | N | - | See Handling Failures |
on_success | N | - | See Handling Success |
tag | N | - | Identifier |
disabled | N | false | When true, the processor is skipped and the event continues to the next one. Lets you take a processor out of the path without removing its configuration |
Details
The processor supports two trimming modes: character-count trimming and keyword trimming. Used together, the count is applied FIRST and the keywords afterwards (trim_last.go, trimLastFromString) — so the keywords are matched against what remains after the count has already been removed, not against the original string.
The processor supports both single strings and string arrays, applying the trimming operation to each string element.
Character count trimming removes the specified number of characters from the end of each string. If the count exceeds the string length, the entire string is removed, resulting in an empty string.
Keyword trimming removes matching suffixes from the end of strings. Multiple keywords can be specified, and each is checked sequentially for suffix matches.
An unparseable count is silently ignored, not rejected: strconv.Atoi is called and its error discarded, leaving the count at 0. A typo produces a pass-through rather than a failure, so nothing signals it. count is also measured in BYTES — on text outside ASCII it does not correspond to characters and can cut one in half.
Examples
Character Count Trimming
Removing last characters from strings... | |
removes the suffixes: | |
Keyword Trimming
Removing specific keywords from end... | |
removes the file extensions: | |
Array Processing
Processing string arrays... | |
removes trailing slashes: | |
File Extension Removal
Removing various file extensions... | |
removes all file extensions: | |
Combined Trimming
Using both keywords and character count... | |
the count goes first, removing the three spaces; the keyword is then trimmed off what is left, so the space before it survives: | |
Conditional Trimming
Trimming based on conditions... | |
applies trimming when condition matches: | |