# Accessing first element after executing split

**URL:** <https://discuss.elastic.co/t/accessing-first-element-after-executing-split/152282>\
**Category:** Logstash\
**Created:** [October 12, 2018, 5:34pm UTC](https://discuss.elastic.co/t/accessing-first-element-after-executing-split/152282 "2018-10-12T17:34:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucas.alvarez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lucas.alvarez/32/33649_2.png) [@lucas.alvarez](https://discuss.elastic.co/u/lucas.alvarez)\
**Post date:** [October 12, 2018, 5:34pm UTC](https://discuss.elastic.co/t/accessing-first-element-after-executing-split/152282/1 "2018-10-12T17:34:52Z")

</div>

Hello everybody!. I cannot find a solution for the following thing. I'm splitting a hostname that is composed by a "name", a "." and a "domain". I would like to get only the name.

I.e: lucas.google =\> lucas

I've read about the split function, which works good, but I cannot find a way to access the first element of the array after performing the split.

This is what I'm using:

> mutate {  
> split =\> { "hostname\_aux" =\> "." }  
> }

This gives me: lucas, google  
I would like to get only lucas in that field.

Thank you!.

---

<div class="post-metadata">

**Author:** ![AquaX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aquax/32/92006_2.png) [@AquaX](https://discuss.elastic.co/u/AquaX)\
**Post date:** [October 13, 2018, 3:14am UTC](https://discuss.elastic.co/t/accessing-first-element-after-executing-split/152282/2 "2018-10-13T03:14:30Z")

</div>

You could use a grok pattern.  
grok{  
match =\> {“hostname\_aux” =\> “%{NOTSPACE:domain}.%{NOTSPACE:name}”}  
}

---

<div class="post-metadata">

**Author:** ![lucas.alvarez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lucas.alvarez/32/33649_2.png) [@lucas.alvarez](https://discuss.elastic.co/u/lucas.alvarez)\
**Post date:** [October 16, 2018, 2:58pm UTC](https://discuss.elastic.co/t/accessing-first-element-after-executing-split/152282/3 "2018-10-16T14:58:22Z")

</div>

> [@AquaX](#):
>
> %{NOTSPACE:domain}.%{NOTSPACE:name}

It does not work. I ended up doing the following:

grok {  
match =\> [  
"[host][name]", "(?\< auxhostname\>[a-zA-Z0-9\_-]+)(.%{GREEDYDATA})?"  
]  
}

Take into consideration that the whitespace between "\<" and "auxhostname" is there because otherwise the word is not visible in this forum.

---

<div class="post-metadata">

**Author:** ![willemdh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/willemdh/32/16922_2.png) [@willemdh](https://discuss.elastic.co/u/willemdh)\
**Post date:** [October 16, 2018, 4:52pm UTC](https://discuss.elastic.co/t/accessing-first-element-after-executing-split/152282/4 "2018-10-16T16:52:55Z")

</div>

Lucas,

Might be too late, seeing you already have a solution, but could just use the mutate with split like this:

```auto
    mutate {
      split => { "hostname_aux" => "." }
      add_field => ["hostname_split" , "%{[hostname_aux][0]}" ]
    }

```

The first element of the array is `%{[hostname_aux][0]}"`

![image](https://us1.discourse-cdn.com/elastic/original/3X/c/e/ceeb577ac82a12a43f8eaf5e2e0eace44341d74e.png)

Grtz

Willem

---

<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:** [November 13, 2018, 4:52pm UTC](https://discuss.elastic.co/t/accessing-first-element-after-executing-split/152282/5 "2018-11-13T16:52:55Z")

</div>

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