Splunk Search

How to cut data in field and display results as a count?

Yy4pb
Explorer

Hello

I have a field called hostName which contains hosts:

host1\user1

host1\user2

host2\user2

host3\user3

And I want to basically do a count of the number of times each host appears:

so:

Host1 = 2

Host2 = 1

Host3 = 1

So I want to cut the data after the backslash (\) and display the host part of the data as a count.

I worked out the regex for this is ".+?\\" on Regex 101 but I am not able to figure it out with the rex field commands

Thank you! 

Labels (1)
0 Karma
1 Solution

gcusello
SplunkTrust
SplunkTrust

Hi @Yy4pb,

please try something like this:

| rex field=your_field "^(?<your_host>[^\\]+)"

Ciao.

Giuseppe

View solution in original post

gcusello
SplunkTrust
SplunkTrust

Hi @Yy4pb,

please try something like this:

| rex field=your_field "^(?<your_host>[^\\]+)"

Ciao.

Giuseppe

Yy4pb
Explorer

EDIT: I had to add more backslashes to the Regex

| rex field=your_field "^(?<your_host>[^\\\\]+)"

Thanks!!

_________________

However when I run your command I get an error

 

Error in 'rex' command: Encountered the following error while compiling the regex '^(?<your_host>[^\]+)': Regex: missing terminating ] for character class.

 

 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hiu @Yy4pb,

you used too many backslashes, the last of tem is considered by Splunk as an escape of ], so the condition in the regex isn't closed.

Try using [^\\] or [^\\\].

Ciao.

Giuseppe

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...