# How to find if grok pattern is matched ot not?

**URL:** <https://discuss.elastic.co/t/how-to-find-if-grok-pattern-is-matched-ot-not/128465>\
**Category:** Logstash\
**Created:** [April 18, 2018, 6:38am UTC](https://discuss.elastic.co/t/how-to-find-if-grok-pattern-is-matched-ot-not/128465 "2018-04-18T06:38:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RameshNagargoje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshnagargoje/32/30212_2.png) [@RameshNagargoje](https://discuss.elastic.co/u/RameshNagargoje)\
**Post date:** [April 18, 2018, 6:38am UTC](https://discuss.elastic.co/t/how-to-find-if-grok-pattern-is-matched-ot-not/128465/1 "2018-04-18T06:38:39Z")

</div>

Hi, I want to find if grok pattern is matched or not.  
If pattern is matched then add one new field in given json else ignore to output it .

here is my input json

{  
"type": "dmesg",  
"hostName": "ramesh-nagargoje",  
"os": "windo",  
"bseid": 1234,  
"logs": "[0.000000] Initializing cgroup subsys cpuset\n[0.000000] Initializing cgroup subsys cpu\n[0.000000] Initializing cgroup subsys cpuacct\n[0.000000] Linux version 3.13.0-144-generic (buildd@lgw01-amd64-059)"  
}

Here is my filter

filter  
{  
if [type] == 'dmesg'  
{

```
        grok
        {
                 match => { [logs] => "Initializing cgroup subsys" }
               add_field => {"tag"=>"tagname1"}
        }

        grok
        {
                 match => { [logs] => "Linux version" }
               add_field => {"tag"=>"tagname2"}
        }

}

```

}

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [April 18, 2018, 6:41am UTC](https://discuss.elastic.co/t/how-to-find-if-grok-pattern-is-matched-ot-not/128465/2 "2018-04-18T06:41:49Z")

</div>

When you use `add_field` like this it'll only trigger when the filter is successful, i.e. the grok filter (in this case) has matched.

If the grok filter fails it'll additionally tag the event with `_grokparsefailure` (unless configured otherwise with the `tag_on_failure` option).

---

<div class="post-metadata">

**Author:** ![RameshNagargoje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshnagargoje/32/30212_2.png) [@RameshNagargoje](https://discuss.elastic.co/u/RameshNagargoje)\
**Post date:** [April 18, 2018, 6:55am UTC](https://discuss.elastic.co/t/how-to-find-if-grok-pattern-is-matched-ot-not/128465/3 "2018-04-18T06:55:48Z")

</div>

thanks @magnusbaeck it worked. But I don’t want to send output to ES if grok is not matched.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [April 18, 2018, 8:37am UTC](https://discuss.elastic.co/t/how-to-find-if-grok-pattern-is-matched-ot-not/128465/4 "2018-04-18T08:37:38Z")

</div>

Then you can use a conditional to either wrap a drop filter or your elasticsearch output.

---

<div class="post-metadata">

**Author:** ![RameshNagargoje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshnagargoje/32/30212_2.png) [@RameshNagargoje](https://discuss.elastic.co/u/RameshNagargoje)\
**Post date:** [April 18, 2018, 10:18am UTC](https://discuss.elastic.co/t/how-to-find-if-grok-pattern-is-matched-ot-not/128465/5 "2018-04-18T10:18:47Z")

</div>

thanks @magnusbaeck it helped a lot.

if "\_grokparsefailure" in [tags]  
{  
drop { }  
}

---

<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:** [May 16, 2018, 10:18am UTC](https://discuss.elastic.co/t/how-to-find-if-grok-pattern-is-matched-ot-not/128465/6 "2018-05-16T10:18:51Z")

</div>

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