# Grok, variable lenght array beside normal fields

**URL:** <https://discuss.elastic.co/t/grok-variable-lenght-array-beside-normal-fields/217864>\
**Category:** Logstash\
**Created:** [February 4, 2020, 7:28pm UTC](https://discuss.elastic.co/t/grok-variable-lenght-array-beside-normal-fields/217864 "2020-02-04T19:28:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Incauto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/incauto/32/60149_2.png) [@Incauto](https://discuss.elastic.co/u/Incauto)\
**Post date:** [February 4, 2020, 7:28pm UTC](https://discuss.elastic.co/t/grok-variable-lenght-array-beside-normal-fields/217864/1 "2020-02-04T19:28:09Z")

</div>

Hi, I have a log tat contains an array and normal fields, separated by a space

```auto
field1 field2 ['27', '32'] field4

```

my grok will be

```auto
%{DATA:field1} %{DATA:field2} ??????? %{DATA:field4} 

```

How can I grok this variable lenght array ?

Thanks

---

<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:** [February 4, 2020, 8:52pm UTC](https://discuss.elastic.co/t/grok-variable-lenght-array-beside-normal-fields/217864/2 "2020-02-04T20:52:44Z")

</div>

I would do it with dissect instead of grok, if you really want to use grok then

```
%{NOTSPACE:field1} %{NOTSPACE:field2} \[%{NOTSPACE:field3}\] %{DATA:field4}

```

might work. Then use mutate+split on field3.

---

<div class="post-metadata">

**Author:** ![Incauto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/incauto/32/60149_2.png) [@Incauto](https://discuss.elastic.co/u/Incauto)\
**Post date:** [February 4, 2020, 9:24pm UTC](https://discuss.elastic.co/t/grok-variable-lenght-array-beside-normal-fields/217864/3 "2020-02-04T21:24:01Z")

</div>

I was trying to follow your answer in another post, but without results, grokparse failure, can you see what is wrong with my code?

this is the log:

```auto

02/04/20 17:07:58 ['somehostgroup', 'Auto Discovery'] --somehost-- 1501869 10255 ['27', '178']

```

The conf

```auto
filter {
        if [application] == "2uptime" {
                grok {
                        match => {"message" => "%{DATA:fecha} %{TIME:hora} \[%{NOTSPACE:hostgroup}\] --%{DATA:hostname}-- %{NUMBER:value} %{NUMBER:hostid} \[%{NOTSPACE:hgid}\]"}
                }

                ruby {
                code => '
                    a = event.get("hostgroup")

                    if a then
                        a.each_index { |i|
                            event.set("hgname#{i+1}", a[i])
                        }
                    end

                    x = event.get("hgid")

                    if x then
                        x.each_index { |z|
                            event.set("hgid#{z+1}", x[z])
                        }
                    end
                '
                }
```

---

<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:** [February 4, 2020, 10:21pm UTC](https://discuss.elastic.co/t/grok-variable-lenght-array-beside-normal-fields/217864/4 "2020-02-04T22:21:59Z")

</div>

> [@Incauto](#):
>
> somehostgroup', 'Auto Discovery'

That contains spaces, so does not match NOTSPACE. Try switching it to DATA.

---

<div class="post-metadata">

**Author:** ![Incauto](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/incauto/32/60149_2.png) [@Incauto](https://discuss.elastic.co/u/Incauto)\
**Post date:** [February 5, 2020, 1:58am UTC](https://discuss.elastic.co/t/grok-variable-lenght-array-beside-normal-fields/217864/5 "2020-02-05T01:58:08Z")

</div>

Thanks Badger but now gives me:

"Ruby exception occurred: undefined method `each\_index' for...the rest of hostgoups in the array"

I've tried removing the brackets surrounding the hostgroup and the hostgroupid,

```auto
\[%{DATA:hostgroup}\]

```

to

```auto
%{DATA:hostgroup}

```

thinking that maybe ruby doesnt know that is an array, but didnt work either

---

<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:** [February 5, 2020, 2:42am UTC](https://discuss.elastic.co/t/grok-variable-lenght-array-beside-normal-fields/217864/6 "2020-02-05T02:42:49Z")

</div>

If you have

> [@Incauto](#):
>
> %{DATA:hostgroup}

The [hostgroup] field is not an array, it is a string. You may be able to convert it to an array using the split function of a mutate filter (not a split filter).

The equivalent of

```
event.get("hostgroup").each_index

```

will throw a undefined method exception unless [hostgroup] is an array.

---

<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:** [March 4, 2020, 2:56am UTC](https://discuss.elastic.co/t/grok-variable-lenght-array-beside-normal-fields/217864/7 "2020-03-04T02:56:38Z")

</div>

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