# Need help with conditional only if field contains a period

**URL:** <https://discuss.elastic.co/t/need-help-with-conditional-only-if-field-contains-a-period/284126>\
**Category:** Logstash\
**Created:** [September 13, 2021, 9:41pm UTC](https://discuss.elastic.co/t/need-help-with-conditional-only-if-field-contains-a-period/284126 "2021-09-13T21:41:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcharles1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pcharles1/32/94519_2.png) [@pcharles1](https://discuss.elastic.co/u/pcharles1)\
**Post date:** [September 13, 2021, 9:41pm UTC](https://discuss.elastic.co/t/need-help-with-conditional-only-if-field-contains-a-period/284126/1 "2021-09-13T21:41:53Z")

</div>

I'm trying to grok the host name field only if it contains a period using the conditional below:  
if ([host][name] =~ ".") { grok host.name field }

For some reason, when the host name field doesn't contain a period, I get \_grokparsefailure. I'm confused as to why this is happening. I would think if there's no period, the conditional won't be satisfied & the grok statement won't be executed. It seems like it is anyway.

I also tried this & it produces the same outcome  
if ([host][name] =~ /./) { grok host.name field }

Any advice?

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [September 13, 2021, 10:22pm UTC](https://discuss.elastic.co/t/need-help-with-conditional-only-if-field-contains-a-period/284126/2 "2021-09-13T22:22:37Z")

</div>

From the [documentation](https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html#conditionals):

> - regexp: `=~` , `!~` (checks a pattern on the right against a string value on the left)

So, the expression `=~ "."` or `=~ /./` will make it match anything.

The conditional that you need to use in this case is:

```auto
if "." in [host][name] { grok [host][name] field }

```

This will check if a literal `.` is a substring of the field `[host][name]`.

---

<div class="post-metadata">

**Author:** ![pcharles1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pcharles1/32/94519_2.png) [@pcharles1](https://discuss.elastic.co/u/pcharles1)\
**Post date:** [September 14, 2021, 1:04pm UTC](https://discuss.elastic.co/t/need-help-with-conditional-only-if-field-contains-a-period/284126/3 "2021-09-14T13:04:44Z")

</div>

That did the trick. Thanks @leandrojmp

---

<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 12, 2021, 1:05pm UTC](https://discuss.elastic.co/t/need-help-with-conditional-only-if-field-contains-a-period/284126/4 "2021-10-12T13:05:13Z")

</div>

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