Splunk Search

## How to split a column into two based on condition?

Explorer

Hi all,

Pls consider this subset of data,

... - Date - Fruit - Seller - Bad_count - ...

11/8 - Apple - X - 3

11/8 - Apple - Y - 10

11/8 - Apple - X - 3

11/8 - Apple - Y - 10

11/8 - Orange - Y - 6

11/8 - Orange - X - 1

11/8 - Orange - Y - 6

11/9 - Apple - X - 0

11/9 - Apple - Y - 9

11/9 - Apple - X - 0

11/9 - Orange - X - 7

11/9 - Orange - Y - 2

I would like to reformat the table into this:

11/8 - Apple - X - 3 — 3 - 10

11/8 - Apple - Y - 10 — 3 - 10

11/8 - Apple - X - 3 — 3 - 10

11/8 - Apple - Y - 10 — 3 - 10

11/8 - Orange - Y - 6 — 1 - 6

11/8 - Orange - X - 1 — 1 - 6

11/8 - Orange - Y - 6 — 1 - 6

11/9 - Apple - X - 0 — 0 - 9

11/9 - Apple - Y - 9 — 0 - 9

11/9 - Apple - X - 0 — 0 - 9

11/9 - Orange - X - 7 — 7 - 2

11/9 - Orange - Y - 2 — 7 - 2

The idea is to split the Bad_count column into two columns based on the unique combination of Date and Fruit.

Any help would be greatly appreciated!

Thanks,

Shrey

PS: 1) There's years of data, many many fruits, and multiple sellers in the original dataset. 2) I've also sorted the sample data by Fruit up there to make it easy to read. 3) Don't worry about the duplicate rows as there are other fields in the dataset as well (meaning, dedup with care).

Labels (9)

• ### table

Tags (3)
1 Solution
Explorer

I was able to solve the problem using @gcusello's snippet along w/ a left join.

``````<base_search>
| bin span=1d _time
| eval column=strftime(_time,"%d/%m")."-".Fruit
| join type=left column
[
| <same_base_search>
| bin span=1d _time
| eval column=strftime(_time,"%d/%m")."-".Fruit
]``````

Thanks, all.

Explorer

I was able to solve the problem using @gcusello's snippet along w/ a left join.

``````<base_search>
| bin span=1d _time
| eval column=strftime(_time,"%d/%m")."-".Fruit
| join type=left column
[
| <same_base_search>
| bin span=1d _time
| eval column=strftime(_time,"%d/%m")."-".Fruit
]``````

Thanks, all.

Motivator

``````| eval {Seller}_bad_count=Bad_count

Explorer

Thanks for the response.

The sum command is not helping in this case. I changed it to values() and it seemed to work but only if I filter for a particular Fruit on a particular day.

Sincerely,

Shrey

Motivator

Are the fieldnames Date, Fruit, Seller, Bad_count?
Could you provide a sample of your data (screenshot)?

Explorer

I'm sorry, I can't leak the exact data as it is critical to the organization. I've masked the use case and the variable names to the best of my ability. However, I can make it clear that the counts require no aggregation, only the table layout needs to be changed.

Motivator

Your table layout example is not clear. Does the dashes between the field values represent artificial delimiters or is it part of the value?

Explorer

I've put dashes there to indicate the separation between the columns. Input table has 4 columns and the output table should have 6 columns (after adding X_bad_count and Y_bad_count). You could ignore the dashes or replace them with | , if it helps.

Motivator

``````| makeresults
| eval data="11/8,Apple,X,3;11/8,Apple,Y,10;11/8,Apple,X,3;11/8,Apple,Y,10;11/8,Orange,Y,6;11/8,Orange,X,1;11/8,Orange,Y,6;11/9,Apple,X,0;11/9,Apple,Y,9;11/9,Apple,X,0;11/9,Orange,X,7;11/9,Orange,Y,2"
| eval data=split(data, ";")
| mvexpand data
| table Date Fruit Seller Bad_count

Explorer

To confirm, @johnhuang - your solution works as well! Thank you!

Explorer

Thanks, I'll try this as well.

SplunkTrust

Hi @shreyp,

you could try something like this:

``````<your_search>
| bin span=1d _time
| eval column=strftime(_time,"%d/%m")."-".Fruit

Ciao.

Giuseppe

Explorer

Hi Giuseppe,

Thanks for the response. This almost did it!

As expected, the chart command outputs in "column - X_bad_count - Y_bad_count" format. How do I append these two count columns back into the main table? I'm looking for a view that we would get from eventstats kind of command.

Thanks,

Shrey

SplunkTrust

Hi @shreyp,

good for you, see next time!

Ciao and happy splunking

Giuseppe

P.S.: Karma Points are appreciated by all the contributors 😉

Get Updates on the Splunk Community!

#### Adoption of RUM and APM at Splunk

Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

#### March Community Office Hours Security Series Uncovered!

Hello Splunk Community! In March, Splunk Community Office Hours spotlighted our fabulous Splunk Threat ...

#### Stay Connected: Your Guide to April Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars in April. This post ...