# Match specific word in grok filter

**URL:** https://discuss.elastic.co/t/match-specific-word-in-grok-filter/192299
**Category:** Logstash
**Created:** [July 25, 2019, 7:00pm UTC](https://discuss.elastic.co/t/match-specific-word-in-grok-filter/192299 "2019-07-25T19:00:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![naveenrt23](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@naveenrt23](https://discuss.elastic.co/u/naveenrt23)
#### Post date: [July 25, 2019, 7:00pm UTC](https://discuss.elastic.co/t/match-specific-word-in-grok-filter/192299/1 "2019-07-25T19:00:35Z")

</div>

I have a log message which is composed of multiple values joined by pipes.  
`20190615|4|method|userend|/test/value/123/1.1|500|2`

mY grok filter:

```
  mutate {
        add_field => {
          "[@metadata][copyOfMessage]" => "%{[message]}"
        }
      }
      # split message
      mutate {
        split => {
          "[@metadata][copyOfMessage]" => "|"
        }
      }
      if [@metadata][copyOfMessage][4] =~ /^\/test/ { 

grok {
    # Enable multiple matchers
    break_on_match => false

    match => { "message" => "%{DATA:timestamp_local}\|%{NUMBER:duration}\|%{WORD:requesttype}\|%{DATA:username}\|%{DATA:resource}\|%{NUMBER:statuscode}\|%{NUMBER:bytes}" }

    # Extract repo and path
    match => { "resource" => "/%{DATA:repo}/%{GREEDYDATA:resource_path}"}

    # Extract resource name
    match => { "resource_path" => "(?<resource_name>[^/]+$)" }
    
}
}

```

For some reason, my IF condition doesn't work perfectly and that block gets executed for every word which starts with "test".

My initial requirement was to send a message through grok filter only if [@metadata][copyOfMessage][4] =~ /^/test/ is true but the filter gets executed if the 5th value is "testing" or "tester". I only need to send the message through the filter if the 5th value is "test"

So,

I have tried something like below,

**[@metadata][copyOfMessage][4] =~ /^/test/ and [@metadata][copyOfMessage][5] =~ /^/value/**  
and the whole block doesnt get exectued.

what can I do to match the exact word "test"?

---

<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: [July 25, 2019, 7:43pm UTC](https://discuss.elastic.co/t/match-specific-word-in-grok-filter/192299/2 "2019-07-25T19:43:37Z")

</div>

> [@naveenrt23](#):
>
> if [@metadata][copyOfMessage][4] =~ /^/test/

This tests whether it matches a regexp. If you want an exact string comparison then use

```
if [@metadata][copyOfMessage][4] == "test"

```

If you want to test whether the string starts with /test/ then use

```
if [@metadata][copyOfMessage][4] =~ /^\/test\//

```

---

<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 22, 2019, 7:43pm UTC](https://discuss.elastic.co/t/match-specific-word-in-grok-filter/192299/3 "2019-08-22T19:43:40Z")

</div>

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