# How to break long condition in logstash config

**URL:** <https://discuss.elastic.co/t/how-to-break-long-condition-in-logstash-config/268709>\
**Category:** Logstash\
**Created:** [March 29, 2021, 9:44pm UTC](https://discuss.elastic.co/t/how-to-break-long-condition-in-logstash-config/268709 "2021-03-29T21:44:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alicango](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alicango/32/84498_2.png) [@alicango](https://discuss.elastic.co/u/alicango)\
**Post date:** [March 29, 2021, 9:44pm UTC](https://discuss.elastic.co/t/how-to-break-long-condition-in-logstash-config/268709/1 "2021-03-29T21:44:14Z")

</div>

I have a filter section that removes a field if the event matches with regex. So my if condition is very long.

```
if [type] == "something" {
    if [displayName] =~ /([M-m]as|MAS)[\d]*/ or [displayName] =~ /([E-e]lk|ELK)/ or [displayName] =~ /other regex/ or [displayName] =~ /other regex/ {
        mutate {
            remove_field => ["[memory]", "[@version]"]
        }
    }
}

```

There are numerous **displayName** values comes from json array. is there another way to handle them? if there is no, how can I break the long **if** condition line?

Many 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:** [March 29, 2021, 10:40pm UTC](https://discuss.elastic.co/t/how-to-break-long-condition-in-logstash-config/268709/2 "2021-03-29T22:40:57Z")

</div>

If you are always matching [displayName] then you could combine the regexps

```
if [displayName] =~ /(([M-m]as|MAS)[\d]*)|([E-e]lk|ELK)|.../

```

If you are OK with using ruby you could try something like [this](https://stackoverflow.com/questions/51308101/can-i-have-an-array-of-regular-expressions-in-ruby).

Or you could use grok

```
grok {
    break_on_match => false
    match => {
        "displayName" => [
            "(?<[@metadata][matched]>([M-m]as|MAS)[\d]*)",
            "(?<[@metadata][matched]>([E-e]lk|ELK)",
            ...
        ]
    }
}
if [@metadata][matched] {
    mutate { remove_field => ["[memory]", "[@version]" "[@metadata][matched]" ] }
}

```

None of which is a great improvement ☹

---

<div class="post-metadata">

**Author:** ![alicango](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alicango/32/84498_2.png) [@alicango](https://discuss.elastic.co/u/alicango)\
**Post date:** [March 30, 2021, 10:34pm UTC](https://discuss.elastic.co/t/how-to-break-long-condition-in-logstash-config/268709/3 "2021-03-30T22:34:11Z")

</div>

They are great improvements. Thank you so much. 🙏 🙂

---

<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 27, 2021, 10:35pm UTC](https://discuss.elastic.co/t/how-to-break-long-condition-in-logstash-config/268709/4 "2021-04-27T22:35:02Z")

</div>

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