Splunk Search

How do you use regex to remove duplicate characters?

mistydennis
Communicator

I have a report that requires several fields to be concatenated, each separated by a semicolon. Because some of the fields are null, the values look like this:

cat; dog; ; ; ; snake; bird; ; hamster; ; ;

I want the field to look like this:

cat; dog; snake; bird; hamster

Is there a way to use regex to remove the duplicate semicolons? I'd also like to make sure the field does not end with a semicolon.

Open to other ideas if regex is not the best solution.

Tags (2)
1 Solution

vnravikumar
Champion

Hi

Try this

| makeresults 
| eval msg="cat; dog; ; ; ; snake; bird; ; hamster; ; ;" 
| makemv delim=";" msg 
| eval msg= ltrim(msg) 
| eval msg=mvjoin(mvfilter(msg!=""),";")

View solution in original post

vnravikumar
Champion

Hi

Try this

| makeresults 
| eval msg="cat; dog; ; ; ; snake; bird; ; hamster; ; ;" 
| makemv delim=";" msg 
| eval msg= ltrim(msg) 
| eval msg=mvjoin(mvfilter(msg!=""),";")

mistydennis
Communicator

YES! Thank you so much!

0 Karma
Get Updates on the Splunk Community!

Pro Tips for First-Time .conf Attendees: Advice from SplunkTrust

Heading to your first .Conf? You’re in for an unforgettable ride — learning, networking, swag collecting, ...

Raise Your Skills at the .conf25 Builder Bar: Your Splunk Developer Destination

Calling all Splunk developers, custom SPL builders, dashboarders, and Splunkbase app creators – the Builder ...

Hunt Smarter, Not Harder: Discover New SPL “Recipes” in Our Threat Hunting Webinar

Are you ready to take your threat hunting skills to the next level? As Splunk community members, you know the ...