# Logstash and Split

**URL:** <https://discuss.elastic.co/t/logstash-and-split/267358>\
**Category:** Logstash\
**Created:** [March 16, 2021, 11:27am UTC](https://discuss.elastic.co/t/logstash-and-split/267358 "2021-03-16T11:27:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![riahc3](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@riahc3](https://discuss.elastic.co/u/riahc3)\
**Post date:** [March 16, 2021, 11:27am UTC](https://discuss.elastic.co/t/logstash-and-split/267358/1 "2021-03-16T11:27:18Z")

</div>

Hello

I have the following fields (data is example):

ForwardedIpAddress 1.2.3.4,4.3.2.1  
IpAddress 1.2.3.4,4.3.2.1

Would I would like to end up with is:

ForwardedIpAddress1 1.2.3.4  
ForwardedIpAddress2 4.3.2.1  
IpAddress1 1.2.3.4  
IpAddress2 4.3.2.1

I think the best method is using the Split plugin BUT I think I need something else to make the result go in new fields

To me it would be something like

```
filter {
 split {
   field => "ForwardedIpAddress"
   target => "ForwardedIpAddress"
 }
}

filter {
 split {
   field => "IpAddress"
   target => "IpAddress"
 }
}

```

But besides this, I dont know how to seperate them into two different fields.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 16, 2021, 3:18pm UTC](https://discuss.elastic.co/t/logstash-and-split/267358/2 "2021-03-16T15:18:26Z")

</div>

I think you would need to use ruby to do that. I have not tested it but something like...

```
ruby {
    code => '
        ["ForwardedIpAddress", "IpAddress"].each { |field|
            f = event.get(field)
            if f
                f = f.split(",")
                f.each_index { |x|
                    event.set("#{field}#{x}", f[x])
                }
            end
        }
    '
}
```

---

<div class="post-metadata">

**Author:** ![riahc3](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@riahc3](https://discuss.elastic.co/u/riahc3)\
**Post date:** [March 16, 2021, 3:37pm UTC](https://discuss.elastic.co/t/logstash-and-split/267358/3 "2021-03-16T15:37:06Z")

</div>

> [@Badger](#):
>
> ```auto
> ruby {
> code => '
> ["ForwardedIpAddress", "IpAddress"].each { |field|
> f = event.get(field)
> if f
> f = f.split(",")
> f.each_index { |x|
> event.set("#{field}#{x}", f[x])
> }
> end
> }
> '
> }
> 
> ```

I can I do it by seperated ? I think it would help me understand it better.

I do not understand Ruby so the code you inputed...I THINK I see a foreach in there but....

---

<div class="post-metadata">

**Author:** ![riahc3](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@riahc3](https://discuss.elastic.co/u/riahc3)\
**Post date:** [March 18, 2021, 2:41pm UTC](https://discuss.elastic.co/t/logstash-and-split/267358/4 "2021-03-18T14:41:21Z")

</div>

This worked but the issue is that I dont understand it well.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 18, 2021, 5:56pm UTC](https://discuss.elastic.co/t/logstash-and-split/267358/5 "2021-03-18T17:56:11Z")

</div>

This performs the contents of the code block (surrounded by {}) for each member of the array ["ForwardedIpAddress", "IpAddress"], substituting the value of the member for the `field` variable.

```
    ["ForwardedIpAddress", "IpAddress"].each { |field|

```

Get the value of the field with the name of the `field` variable (i.e., first [ForwardedIpAddress], then in the next iteration [IpAddress]) and assign it to `f`

```
        f = event.get(field)

```

Verify that the field exists. If it does not then event.get returns nil and the if will test false

```
        if f

```

Split the field into an array using a comma as the separator

```
            f = f.split(",")

```

For each member of that array

```
            f.each_index { |x|

```

Set a field on the event with the name ForwardedIpAddress0 equal to the first member of the array derived from ForwardedIpAddress etc. This is [string interpolation](http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html).

```
                event.set("#{field}#{x}", f[x])
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 15, 2021, 5:56pm UTC](https://discuss.elastic.co/t/logstash-and-split/267358/6 "2021-04-15T17:56:44Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
