# How to use grok to filter some fields in message in logstash, skip the unnecessary, only filter the specified

**URL:** <https://discuss.elastic.co/t/how-to-use-grok-to-filter-some-fields-in-message-in-logstash-skip-the-unnecessary-only-filter-the-specified/142754>\
**Category:** Logstash\
**Created:** [August 2, 2018, 11:25am UTC](https://discuss.elastic.co/t/how-to-use-grok-to-filter-some-fields-in-message-in-logstash-skip-the-unnecessary-only-filter-the-specified/142754 "2018-08-02T11:25:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sun\_changlong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sun_changlong/32/39036_2.png) [@sun\_changlong](https://discuss.elastic.co/u/sun_changlong)\
**Post date:** [August 2, 2018, 11:25am UTC](https://discuss.elastic.co/t/how-to-use-grok-to-filter-some-fields-in-message-in-logstash-skip-the-unnecessary-only-filter-the-specified/142754/1 "2018-08-02T11:25:34Z")

</div>

For example, the data format is as follows:

> dev="1235667" pri="6" dev\_type="ddos\_clean" recorder="ads" vsid="0" sub\_type=attacklog dst\_addr=1.1.1.1 zonename=1504152157558 grpname=test

Just get the contents of the sub\_type field, skip all other, not match each field。Because the message data format is random, i can't use fixed filtering rules. How can I achieve this?

---

<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:** [August 2, 2018, 11:28am UTC](https://discuss.elastic.co/t/how-to-use-grok-to-filter-some-fields-in-message-in-logstash-skip-the-unnecessary-only-filter-the-specified/142754/2 "2018-08-02T11:28:25Z")

</div>

Use a kv filter and its `include_keys` option to ignore everything but the "sub\_type" key.

---

<div class="post-metadata">

**Author:** ![sun\_changlong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sun_changlong/32/39036_2.png) [@sun\_changlong](https://discuss.elastic.co/u/sun_changlong)\
**Post date:** [August 2, 2018, 11:36am UTC](https://discuss.elastic.co/t/how-to-use-grok-to-filter-some-fields-in-message-in-logstash-skip-the-unnecessary-only-filter-the-specified/142754/3 "2018-08-02T11:36:05Z")

</div>

Thank you, I know this way, but I don't know how to deal with grok. I only see that the grok can be processed one by one. I don't see how to implement the ignore skip processing.Do you know the method of grok?

---

<div class="post-metadata">

**Author:** ![sun\_changlong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sun_changlong/32/39036_2.png) [@sun\_changlong](https://discuss.elastic.co/u/sun_changlong)\
**Post date:** [August 2, 2018, 11:38am UTC](https://discuss.elastic.co/t/how-to-use-grok-to-filter-some-fields-in-message-in-logstash-skip-the-unnecessary-only-filter-the-specified/142754/4 "2018-08-02T11:38:40Z")

</div>

If the message format is not key-value, can it only be processed with grok? I want to know how grok is implemented.

---

<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:** [August 2, 2018, 12:04pm UTC](https://discuss.elastic.co/t/how-to-use-grok-to-filter-some-fields-in-message-in-logstash-skip-the-unnecessary-only-filter-the-specified/142754/5 "2018-08-02T12:04:55Z")

</div>

A grok expression is basically a regular expression so you can use wildcards to ignore the parts you're not interested in. The grok expression `sub_type=%{WORD:subtype}` will find a subtype anywhere in the input string and extract a field from it, but it will also find bogus string like "whatever\_sub\_type=foo" so some care is needed.

---

<div class="post-metadata">

**Author:** ![sun\_changlong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sun_changlong/32/39036_2.png) [@sun\_changlong](https://discuss.elastic.co/u/sun_changlong)\
**Post date:** [August 3, 2018, 3:54am UTC](https://discuss.elastic.co/t/how-to-use-grok-to-filter-some-fields-in-message-in-logstash-skip-the-unnecessary-only-filter-the-specified/142754/6 "2018-08-03T03:54:37Z")

</div>

thank you ,i'm try again

---

<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:** [August 31, 2018, 3:54am UTC](https://discuss.elastic.co/t/how-to-use-grok-to-filter-some-fields-in-message-in-logstash-skip-the-unnecessary-only-filter-the-specified/142754/7 "2018-08-31T03:54:38Z")

</div>

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