# Logstash grok - How to match not(negate) the pattern in grok

**URL:** https://discuss.elastic.co/t/logstash-grok-how-to-match-not-negate-the-pattern-in-grok/131922
**Category:** Logstash
**Created:** [May 15, 2018, 11:55am UTC](https://discuss.elastic.co/t/logstash-grok-how-to-match-not-negate-the-pattern-in-grok/131922 "2018-05-15T11:55:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![arun\_prasath1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arun_prasath1/32/49539_2.png) [@arun\_prasath1](https://discuss.elastic.co/u/arun_prasath1)
#### Post date: [May 15, 2018, 11:55am UTC](https://discuss.elastic.co/t/logstash-grok-how-to-match-not-negate-the-pattern-in-grok/131922/1 "2018-05-15T11:55:16Z")

</div>

I have bunch of log files which are named using its version numbers like below. To match the filename having compA and 2-0/3-0 version, I have like below

projectA-compA-test.log  
projectA-compA-2-0-test.log  
projectA-compA-3-0-test.log  
projectA-compA-feature-test.log

Grok/Logstash configuration:

> input { }
> 
> if [source] =~ "projectA-compA-2-0" {  
> ...  
> } else if [source] =~ "projectA-3-0" {  
> ...  
> } else if [source] =~ "projectA-compA-feature" {  
> ...  
> } else if ! [source] =~ "projectA-compA-2-0" and ! [source] =~ "projectA-compA-3-0" and ! [source] =~ "projectA-compA-feature" {  
> ...  
> }

Please suggest whether my negate condition is correct. Is there any other best way to achieve negate condition.

output { }

---

<div class="post-metadata">

### Author: ![paz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paz/32/28003_2.png) [@paz](https://discuss.elastic.co/u/paz)
#### Post date: [May 15, 2018, 12:03pm UTC](https://discuss.elastic.co/t/logstash-grok-how-to-match-not-negate-the-pattern-in-grok/131922/2 "2018-05-15T12:03:48Z")

</div>

Regex negation is defined as such (you can see all appropriate operators [here](https://www.elastic.co/guide/en/logstash/master/event-dependent-configuration.html))

```auto
...
} else if [source] !~ "projectA-compA-2-0" and [source] !~ "projectA-compA-3-0" and [source] !~ "projectA-compA-feature" {
   ...
}
```

---

<div class="post-metadata">

### Author: ![arun\_prasath1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arun_prasath1/32/49539_2.png) [@arun\_prasath1](https://discuss.elastic.co/u/arun_prasath1)
#### Post date: [May 15, 2018, 3:19pm UTC](https://discuss.elastic.co/t/logstash-grok-how-to-match-not-negate-the-pattern-in-grok/131922/3 "2018-05-15T15:19:08Z")

</div>

Thanks Paz. Its working now.

---

<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 12, 2018, 3:19pm UTC](https://discuss.elastic.co/t/logstash-grok-how-to-match-not-negate-the-pattern-in-grok/131922/4 "2018-06-12T15:19:10Z")

</div>

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