# How to build grok filter

**URL:** https://discuss.elastic.co/t/how-to-build-grok-filter/304537
**Category:** Logstash
**Created:** [May 12, 2022, 8:14am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537 "2022-05-12T08:14:02Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![smam](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@smam](https://discuss.elastic.co/u/smam)
#### Post date: [May 12, 2022, 8:14am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/1 "2022-05-12T08:14:02Z")

</div>

Hello,  
I want to understand how to build grok filter since other posts did not make it clear enough for me.

Here are example lines I want to send:

```auto
30,05/10/22,07:30:27,DNS Update Request,<ip>,<name>,,,0,6,,,,,,,,,0
11,05/10/22,07:30:27,Renew,<ip>,<name>,04EA56A998EA,,303150361,0,,,,<MAC>.0,,,,0
31,05/10/22,07:30:27,DNS Update Failed,<IP>,<name>,,0,6,,,,,,,,,9004

```

Here is the filter i built:

```auto
input {
  file {
        path => ["/etc/filebeat/dhcp/test.log"]
        add_field => { "testvalue" => "Shipped with logstash :)" }
        start_position => "beginning"
  }
}

filter {
  grok{
        match => { "message" => "%{code} %{DATE} %{TIME} %{EVENT} %{IP} %{NAME}" }
  }
  if "DNS" in [EVENT] {
        drop {
                                                                    
        }
  }
}

output {
  elasticsearch {
    hosts => "https://elasticsearch:9200"
    cacert => '/ca.crt'
    user => 'logstash'
    password => ?
    index => "logstash-%{+yyyy.MM.dd}-dhcp"
  }
}

```

I would like to filter the log lines in a way, in which it is represented as:

```auto
Code: x
Date: x
Time: x
IP: x
Name: x

```

Because currently it is one line(1:1 the same line as in the log file), which makes it hard to filter and work with

---

<div class="post-metadata">

### Author: ![Rios](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rios/32/95745_2.png) [@Rios](https://discuss.elastic.co/u/Rios)
#### Post date: [May 12, 2022, 8:23am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/2 "2022-05-12T08:23:12Z")

</div>

Make your life easier, use csv filter plugin.

---

<div class="post-metadata">

### Author: ![ibra\_013](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ibra_013/32/104827_2.png) [@ibra\_013](https://discuss.elastic.co/u/ibra_013)
#### Post date: [May 12, 2022, 8:32am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/3 "2022-05-12T08:32:10Z")

</div>

Hi,

It will be like this using Grok, but there is other options like dissect or csv

this [link](https://grokdebug.herokuapp.com/) is your friend for grok.

```auto
filter {
  grok{
        match => { "message" => "%{NUMBER:code},%{DATE:Date},%{TIME:Time},%{DATA:EVENT},%{IP:Ip},%{WORD:NAME},%{GREEDYDATA:Rest}" }
  }
  if "DNS" in [EVENT] {
        drop {
                                                                    
        }
  }
}

```

---

<div class="post-metadata">

### Author: ![smam](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@smam](https://discuss.elastic.co/u/smam)
#### Post date: [May 12, 2022, 8:39am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/4 "2022-05-12T08:39:32Z")

</div>

Hello, thank you. The pattern you provided "does not match the input data" but I got a feeling for how to construct the filter. With you showing me to assign Datatypes and seperate them by, in this case, ",". I will now try to make it work and then see if the csv filter makes it easier. Thank you!

---

<div class="post-metadata">

### Author: ![ibra\_013](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ibra_013/32/104827_2.png) [@ibra\_013](https://discuss.elastic.co/u/ibra_013)
#### Post date: [May 12, 2022, 8:42am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/5 "2022-05-12T08:42:06Z")

</div>

Hi,

in what does not match the input data?

---

<div class="post-metadata">

### Author: ![smam](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@smam](https://discuss.elastic.co/u/smam)
#### Post date: [May 12, 2022, 8:44am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/6 "2022-05-12T08:44:14Z")

</div>

If I paste the lines into "Sample data" and your filter into "Grok Pattern":  
After clicking "Simulate" I get a pop-up saying: Provided Grok patterns do not match data in the input

---

<div class="post-metadata">

### Author: ![ibra\_013](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ibra_013/32/104827_2.png) [@ibra\_013](https://discuss.elastic.co/u/ibra_013)
#### Post date: [May 12, 2022, 8:47am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/7 "2022-05-12T08:47:41Z")

</div>

Hi,

i used this input data ;

```auto
30,05/10/22,07:30:27,DNS Update Request,10.0.0.0,user,,0,6,,,,,,,,,0

```

---

<div class="post-metadata">

### Author: ![smam](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@smam](https://discuss.elastic.co/u/smam)
#### Post date: [May 12, 2022, 8:56am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/8 "2022-05-12T08:56:08Z")

</div>

Hmm strange..I do not know why its working for you and not for me..

Even if I paste your input data, I get the same error

---

<div class="post-metadata">

### Author: ![smam](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@smam](https://discuss.elastic.co/u/smam)
#### Post date: [May 12, 2022, 10:17am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/9 "2022-05-12T10:17:41Z")

</div>

![grafik](https://us1.discourse-cdn.com/elastic/original/3X/e/c/ecf74abf452718b2eb8e8214295514c5abfe9257.png)

This should be right?

---

<div class="post-metadata">

### Author: ![ibra\_013](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ibra_013/32/104827_2.png) [@ibra\_013](https://discuss.elastic.co/u/ibra_013)
#### Post date: [May 12, 2022, 10:29am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/10 "2022-05-12T10:29:51Z")

</div>

Hi,

Grok debugger is used only to check the grok part, what you are trying to do on the screenshot is on the Logstash.

---

<div class="post-metadata">

### Author: ![smam](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@smam](https://discuss.elastic.co/u/smam)
#### Post date: [May 12, 2022, 10:31am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/11 "2022-05-12T10:31:42Z")

</div>

I see. I made it work just fine with csv, but I will try to also understand logstash grok filter. Thank you

---

<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: [June 9, 2022, 10:32am UTC](https://discuss.elastic.co/t/how-to-build-grok-filter/304537/12 "2022-06-09T10:32:17Z")

</div>

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