MetricsHub
MetricsHub Community Connectors 1.0.22
-
Home
- Connector Developer Guide Computes 24
extract (Compute)
When To Use
Use extract when a single column packs several pieces of information into one string — Model|Size, 2.5 GHz, Name,Instance — and you only need one of them. The compute splits the column value into sub-columns using the subSeparators characters and replaces the column, in place, with the sub-column selected by subColumn. All other columns and all rows are untouched.
For extracting a fixed-position slice of a string, see substring; for extracting a key="value" property from a WBEM object path, see extractPropertyFromWbemPath[1].
Syntax
sources:
diskInventory:
type: commandLine
commandLine: ${file::list-disks.sh}
computes:
# Column 5 contains "Model|Size"; keep only "Model"
- type: extract
column: 5
subColumn: 1
subSeparators: '|'
Properties
| Property | Required | Default | Description |
|---|---|---|---|
type |
Yes | None | extract. |
column |
Yes | None | 1-based index of the column to split. The extracted sub-part replaces this column's value in place. |
subColumn |
Yes | None | 1-based index of the sub-part to keep after splitting. If the separator never occurs in the value, the whole value is sub-column 1. |
subSeparators |
Yes | None | One or more separator characters. Each character in this string is an individual separator (like awk -F"[...]"), not a multi-character delimiter. Consecutive separators delimit empty sub-columns, which still count. Quote values that are special in YAML, e.g. '|', '"', ','. |
If column points past the end of a row, or a value in that column is missing, the compute logs a warning and leaves the whole table unchanged.
Table Transformation Example
With column: 5, subColumn: 1, subSeparators: '|':
Input
| ID | Vendor | Serial | Status | Model | Size |
|---|---|---|---|---|
| disk0 | Seagate | ZC11ABC | ok | ST4000NM|4000 |
| disk1 | WDC | WX21DEF | ok | WD40EFRX|4000 |
Result
| ID | Vendor | Serial | Status | Model |
|---|---|---|---|---|
| disk0 | Seagate | ZC11ABC | ok | ST4000NM |
| disk1 | WDC | WX21DEF | ok | WD40EFRX |
Serialized, disk0;Seagate;ZC11ABC;ok;ST4000NM|4000 becomes disk0;Seagate;ZC11ABC;ok;ST4000NM.
Recommended Pattern
- Chain two
extractcomputes to unpack a two-in-one column: firstduplicateColumn, then extract sub-column 1 from the original and sub-column 2 from the copy (seeIpmiTool, which splitsModel|Sizethis way). - Quote
subSeparatorswhenever the character is meaningful in YAML ('|','"',':',' '). - Extract before numeric computes (
multiply,divide):2.5 GHzmust become2.5before you can do arithmetic on it.
Common Mistakes
- Treating
subSeparatorsas one multi-character delimiter:subSeparators: ', 'splits on comma and on space independently. - Writing the property name as
subSeparator(singular) — the correct property issubSeparatorsand a misspelled property is rejected by the schema. - Forgetting that empty sub-parts count: with
subSeparators: ':', the valuea::bhas sub-columnsa, `` (empty) andb, sosubColumn: 3yieldsb. - Expecting the split parts to be appended as new columns;
extractreplaces the original column with the single selected sub-part.
Community Examples
- IpmiTool[2]
- Windows[3]
- WBEMGenDiskNT[4]
From IpmiTool (column 5 contains Model|Size; duplicate the column, then keep the model from the original and the size from the copy):
# Duplicate the "Model" column because it is in the form of Model|Speed
# Memory module;DeviceID;Entity ID;Vendor;Model|Speed;Model|Speed;SerialNumber;StatusArray;StatusInformation;AdditionalInformation1;
- type: duplicateColumn
column: 5
# Now extract "Model" from "Model|Size"
# Memory module;DeviceID;Entity ID;Vendor;Model;Model|Speed;SerialNumber;StatusArray;StatusInformation;AdditionalInformation1;
- type: extract
column: 5
subColumn: 1
subSeparators: '|'
# Now extract "Size" from "Model|Size"
# Memory module;DeviceID;Entity ID;Vendor;Model;Speed;SerialNumber;StatusArray;StatusInformation;AdditionalInformation1;
- type: extract
column: 6
subColumn: 2
subSeparators: '|'
- [1] extract-wbem-property.html
- [2] https://github.com/metricshub/community-connectors/blob/main/src/main/connector/hardware/IpmiTool/IpmiTool.yaml
- [3] https://github.com/metricshub/community-connectors/blob/main/src/main/connector/system/Windows/Windows.yaml
- [4] https://github.com/metricshub/community-connectors/blob/main/src/main/connector/hardware/WBEMGenDiskNT/WBEMGenDiskNT.yaml
