Splunk Search

How do I use regular expressions to populate a new field if the regular expression is true?

russell120
Communicator

Here's an example of my CSV with 10s of thousand of rows:

device      ID
phone  [APPLE]1234
phone  [ANDROID]0987
phone  [ANDROID]4466
phone  [APPLE]9922

How do I use the regex command to look in the ID field and populate a new field called newField1 with a value of eligiblePhone if the ID value contains [APPLE]; and to also populate a new field called newField2 with a value of ineligiblePhone if the ID value contains [ANDROID]?

The result would look like the below:

device      ID            newField1       newField2
phone  [APPLE]1234      eligiblePhone
phone  [ANDROID]0987                    ineligiblePhone
phone  [ANDROID]4466                    ineligiblePhone
phone  [APPLE]9922      eligiblePhone

Thank you!

0 Karma
1 Solution

dmarling
Builder

The match command in an eval statement will accomplish this:

| eval newField1=if(match(ID, "APPLE"), "eligiblePhone", null())
| eval newField2=if(match(ID, "ANDROID"), "ineligiblePhone", null())

The documentation for that can be found here: https://docs.splunk.com/Documentation/Splunk/6.6.6/SearchReference/ConditionalFunctions#match.28SUBJ...

If this comment/answer was helpful, please up vote it. Thank you.

View solution in original post

0 Karma

dmarling
Builder

The match command in an eval statement will accomplish this:

| eval newField1=if(match(ID, "APPLE"), "eligiblePhone", null())
| eval newField2=if(match(ID, "ANDROID"), "ineligiblePhone", null())

The documentation for that can be found here: https://docs.splunk.com/Documentation/Splunk/6.6.6/SearchReference/ConditionalFunctions#match.28SUBJ...

If this comment/answer was helpful, please up vote it. Thank you.
0 Karma

russell120
Communicator

This works, thanks!

0 Karma
Get Updates on the Splunk Community!

Updated Team Landing Page in Splunk Observability

We’re making some changes to the team landing page in Splunk Observability, based on your feedback. The ...

New! Splunk Observability Search Enhancements for Splunk APM Services/Traces and ...

Regardless of where you are in Splunk Observability, you can search for relevant APM targets including service ...

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...