# Remove trailing white space in logstash filter

**URL:** <https://discuss.elastic.co/t/remove-trailing-white-space-in-logstash-filter/110819>\
**Category:** Logstash\
**Created:** [December 8, 2017, 10:26am UTC](https://discuss.elastic.co/t/remove-trailing-white-space-in-logstash-filter/110819 "2017-12-08T10:26:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![platso588](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@platso588](https://discuss.elastic.co/u/platso588)\
**Post date:** [December 8, 2017, 10:26am UTC](https://discuss.elastic.co/t/remove-trailing-white-space-in-logstash-filter/110819/1 "2017-12-08T10:26:14Z")

</div>

I am trying to add filter for the below log format:

[24/Oct/2017 15:04:53] cluster WARNING Picking RM HA: ha

The filter I have added is:  
[%{MONTHDAY:logDate}/%{MONTH:logMonth}/%{YEAR:logYear} %{TIME:logTime}]%{SPACE}%{GREEDYDATA:platformType} +\s %{SPACE}%{LOGLEVEL:logLevel}%{SPACE}%{GREEDYDATA:message}

Issue is for platformType - cluster has trailing space and I would like to remove. Below is the screenshot from [https://grokdebug.herokuapp.com/](https://grokdebug.herokuapp.com/)

Can any one help me in fetching only the platformType value by removing trailing spaces.

Thanks in Advance.

 ![grok](https://us1.discourse-cdn.com/elastic/original/3X/0/8/080bedf49110c531c6bcb54e4ae4b27600575dcb.png)

---

<div class="post-metadata">

**Author:** ![paz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paz/32/28003_2.png) [@paz](https://discuss.elastic.co/u/paz)\
**Post date:** [December 8, 2017, 10:47am UTC](https://discuss.elastic.co/t/remove-trailing-white-space-in-logstash-filter/110819/2 "2017-12-08T10:47:08Z")

</div>

What are the possible values of _platformType_ ? If all values are only a single word, you'd be better off using something like _NOTSPACE_ instead of _GREEDYDATA_ (since greedydata is a bit expensive). Also, you should escape those brackets in your pattern.

This should work:

```auto
\[%{MONTHDAY:logDate}/%{MONTH:logMonth}/%{YEAR:logYear} %{TIME:logTime} \] %{GREEDYDATA:platformType} %{LOGLEVEL:logLevel} %{GREEDYDATA:message}
```

---

<div class="post-metadata">

**Author:** ![platso588](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@platso588](https://discuss.elastic.co/u/platso588)\
**Post date:** [December 8, 2017, 11:19am UTC](https://discuss.elastic.co/t/remove-trailing-white-space-in-logstash-filter/110819/3 "2017-12-08T11:19:55Z")

</div>

> [@platso588](#):
>
> [24/Oct/2017 15:04:53] cluster WARNING Picking RM HA: ha

Sorry,  
This did not work for me. A clarification I have copy pasted the log while logging this and there are 6 spaces between the words cluster and WARNING and these were removed on submission.

Can you try by explicitely adding 6 spaces between cluster and WARNING in the below log message before trying in [https://grokdebug.herokuapp.com/](https://grokdebug.herokuapp.com/).

[24/Oct/2017 15:04:53] cluster WARNING Picking RM HA: ha

Sorry for this.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![paz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paz/32/28003_2.png) [@paz](https://discuss.elastic.co/u/paz)\
**Post date:** [December 8, 2017, 11:29am UTC](https://discuss.elastic.co/t/remove-trailing-white-space-in-logstash-filter/110819/4 "2017-12-08T11:29:12Z")

</div>

Ah, now I see the issue appearing indeed. This should work

```auto
\[%{MONTHDAY:logDate}/%{MONTH:logMonth}/%{YEAR:logYear} %{TIME:logTime} \] %{NOTSPACE:platformType}\s+%{LOGLEVEL:logLevel} %{GREEDYDATA:message}
```

Also, you can always remove leading/trailing whitespace post-grok with the [strip](https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-strip) option of the mutate filter, if you want to avoid trying to track down all possible corner cases for grok patterns.

---

<div class="post-metadata">

**Author:** ![platso588](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@platso588](https://discuss.elastic.co/u/platso588)\
**Post date:** [December 8, 2017, 11:56am UTC](https://discuss.elastic.co/t/remove-trailing-white-space-in-logstash-filter/110819/5 "2017-12-08T11:56:58Z")

</div>

> [@paz](#):
>
> [%{MONTHDAY:logDate}/%{MONTH:logMonth}/%{YEAR:logYear} %{TIME:logTime}] %{NOTSPACE:platformType}\s+%{LOGLEVEL:logLevel} %{GREEDYDATA:message}

Thanks a lot. It worked with NOTSPACE as suggested by you. However, I tried strip and was unsuccessful. Is there any snippet for this same trail space removal.

---

<div class="post-metadata">

**Author:** ![paz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paz/32/28003_2.png) [@paz](https://discuss.elastic.co/u/paz)\
**Post date:** [December 8, 2017, 12:00pm UTC](https://discuss.elastic.co/t/remove-trailing-white-space-in-logstash-filter/110819/6 "2017-12-08T12:00:25Z")

</div>

This should work if you place it after your grok filter.

```auto
mutate { 
  strip => ["platformType"] 
}
```

---

<div class="post-metadata">

**Author:** ![platso588](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@platso588](https://discuss.elastic.co/u/platso588)\
**Post date:** [December 8, 2017, 12:25pm UTC](https://discuss.elastic.co/t/remove-trailing-white-space-in-logstash-filter/110819/7 "2017-12-08T12:25:53Z")

</div>

Thanks a lot. This worked for me.

---

<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:** [January 5, 2018, 12:25pm UTC](https://discuss.elastic.co/t/remove-trailing-white-space-in-logstash-filter/110819/8 "2018-01-05T12:25:55Z")

</div>

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