foreach
Description
Use this command to run a streaming subsearch that uses a template to iterate over each field in a wildcarded field list.
Syntax
The required syntax is in bold.
- foreach
- <wc-field-list>
- [fieldstr=<string>]
- [matchstr=<string>]
- [matchseg1=<string>]
- [matchseg2=<string>]
- [matchseg3=<string>]
- <subsearch>
Required arguments
- <wc-field-list>
- Syntax: <field> ...
- Description: A space-delimited list of valid field names. You can use the asterisk ( * ) as a wildcard to specify a list of fields with similar names. For example, if you want to specify all fields that start with "value", you can use a wildcard such as
value*
.
- subsearch
- Syntax: [ subsearch ]
- Description: A subsearch that includes a template for replacing the values of the fields specified. The subsearch can use the following tokens:
Token Description <<FIELD>> Each time the subsearch is run, the field value substitutes the whole field name for each field you specify in the <field-list>. <<MATCHSTR>> The part of field name that matches wildcards in the specifier. <<MATCHSEG1>> The part of field name that matches first wildcard. <<MATCHSEG2>> The part of field name that matches second wildcard. <<MATCHSEG3>> The part of field name that matches third wildcard.
Optional arguments
- fieldstr
- Syntax: fieldstr=<string>
- Description: Replaces the <<FIELD>> token with the whole field name.
- matchstr
- Syntax: matchstr=<string>
- Description: Replaces <<MATCHSTR>> with part of the field name that matches wildcard(s) in the specifier.
- matchseg1
- Syntax: matchseg1=<string>
- Description: Replaces <<MATCHSEG1>> with part of the field name that matches the first wildcard.
- matchseg2
- Syntax: matchseg2=<string>
- Description: Replaces <<MATCHSEG2>> with part of the field name that matches the second wildcard.
- matchseg3
- Syntax: matchseg3=<string>
- Description: Replaces <<MATCHSEG3>> with part of the field name that matches the third wildcard.
Usage
If the field names contain characters other than alphanumeric characters, such as dashes, underscores, or periods, you need to enclose the <<FIELD>> token in single quotation marks in the eval
command portion of the search.
For example, the following search adds the values from all of the fields that start with similar names.
... | eval total=0 | eval test_1=1 | eval test_2=2 | eval test_3=3 | foreach test* [eval total=total + '<<FIELD>>']
The <<FIELD>> token in the foreach
subsearch is just a string replacement of the field names test*
. The eval expression does not recognize field names with non-alphanumeric characters unless the field names are surrounded by single quotation marks. For the eval expression to work, the <<FIELD>> token needs to be surrounded by single quotation marks.
Examples
1. Add the values from all of the fields that start with similar names
The following search adds the values from all of the fields that start with similar names. You can run this search on your own Splunk instance.
|makeresults 1| eval total=0 | eval test1=1 | eval test2=2 | eval test3=3 | foreach test* [eval total=total + <<FIELD>>]
- This search creates 1 result using the
makeresults
command. - The search then uses the
eval
command to create the fieldstotal
,test1
,test2
, andtest3
with corresponding values. - The
foreach
command is used to perform the subsearch for every field that starts with "test". Each time the subsearch is run, the previous total is added to the value of the test field to calculate the new total. The final total after all of the "test" fields are processed is 6.
The following table shows how the subsearch iterates over each "test" field. The table shows the beginning value of the "total" field each time the subsearch is run and the calculated total based on the value for the "test" field.
Subsearch iteration | Test field | Total field start value | Test field value | Calculation of "total" field |
---|---|---|---|---|
1 | test1 | 0 | 1 | 0+1=1 |
2 | test2 | 1 | 2 | 1+2=3 |
3 | test3 | 3 | 3 | 3+3=6 |
2. Monitor license usage
Use the foreach
command to monitor license usage.
First run the following search on the license master to return the daily license usage per sourcetype in bytes:
index=_internal source=*license_usage.log type!="*Summary" earliest=-30d
| timechart span=1d sum(b) AS daily_bytes by st
Use the foreach command to calculate the daily license usage in gigabytes for each field:
index=_internal source=*license_usage.log type!="*Summary" earliest=-30d
| timechart span=1d sum(b) AS daily_bytes by st
| foreach * [eval <<FIELD>>='<<FIELD>>'/1024/1024/1024]
3. Use the <<MATCHSTR>>
Add each field that matches foo*
to the corresponding bar*
and write the result to a new_*
field. For example, new_X = fooX + barX.
... | foreach foo* [eval new_<<MATCHSTR>> = <<FIELD>> + bar<<MATCHSTR>>]
4.
Equivalent to ... | eval foo="foo" | eval bar="bar" | eval baz="baz"
... | foreach foo bar baz [eval <<FIELD>> = "<<FIELD>>"]
5.
For the field, fooXbarY, this is equivalent to: ... | eval fooXbarY = "Y"
... | foreach foo*bar* fieldstr="#field#" matchseg2="#matchseg2#" [eval #field# = "#matchseg2#"]
See also
folderize | format |
This documentation applies to the following versions of Splunk® Enterprise: 8.1.1, 8.1.2, 8.1.3, 8.1.4, 8.1.5, 8.1.6, 8.1.7, 8.1.8, 8.1.9, 8.1.10, 8.1.11, 8.1.12, 8.1.13, 8.1.14, 8.2.0, 8.2.1, 8.2.2, 8.2.3, 8.2.4, 8.2.5, 8.2.6, 8.2.7, 8.2.8, 8.2.9, 8.2.10, 8.2.11, 8.2.12
Feedback submitted, thanks!