# Logstash: Fetch the end string using grok or other filters

**URL:** <https://discuss.elastic.co/t/logstash-fetch-the-end-string-using-grok-or-other-filters/198448>\
**Category:** Logstash\
**Created:** [September 6, 2019, 4:16pm UTC](https://discuss.elastic.co/t/logstash-fetch-the-end-string-using-grok-or-other-filters/198448 "2019-09-06T16:16:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chimbu3306](https://avatars.discourse-cdn.com/v4/letter/c/c67d28/32.png) [@chimbu3306](https://discuss.elastic.co/u/chimbu3306)\
**Post date:** [September 6, 2019, 4:16pm UTC](https://discuss.elastic.co/t/logstash-fetch-the-end-string-using-grok-or-other-filters/198448/1 "2019-09-06T16:16:06Z")

</div>

I have a field in my message where I only need the last part of the string but the message length varies for different environments. I tried using grok to fetch the last part of the message but unable to get the expected output

input log

"loggroup" : "test-environment-qa"

or

"loggroup" : "test-dev"

or

"loggroup" : "authentication-group-env-perf"

expected output:

"loggroup" : "qa"

or

"loggroup" : "dev"

or

"loggroup" : "perf"

how to get the expected output in logstash using filters ?

---

<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:** [September 6, 2019, 6:02pm UTC](https://discuss.elastic.co/t/logstash-fetch-the-end-string-using-grok-or-other-filters/198448/2 "2019-09-06T18:02:16Z")

</div>

Use mutate+gsub

```
mutate { gsub => ["loggroup", ".*\-([^-]+)$", "\1" ] }

```

The \1 references the capture group (the stuff inside parentheses) which is one or more characters that are not -, anchored to the end of the line.

---

<div class="post-metadata">

**Author:** ![chimbu3306](https://avatars.discourse-cdn.com/v4/letter/c/c67d28/32.png) [@chimbu3306](https://discuss.elastic.co/u/chimbu3306)\
**Post date:** [September 12, 2019, 9:16am UTC](https://discuss.elastic.co/t/logstash-fetch-the-end-string-using-grok-or-other-filters/198448/3 "2019-09-12T09:16:45Z")

</div>

Thanks @Badger

---

<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:** [October 10, 2019, 9:23am UTC](https://discuss.elastic.co/t/logstash-fetch-the-end-string-using-grok-or-other-filters/198448/4 "2019-10-10T09:23:42Z")

</div>

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