# How to define patterns\_dir as global in logstash...?

**URL:** <https://discuss.elastic.co/t/how-to-define-patterns-dir-as-global-in-logstash/188812>\
**Category:** Logstash\
**Created:** [July 3, 2019, 9:14pm UTC](https://discuss.elastic.co/t/how-to-define-patterns-dir-as-global-in-logstash/188812 "2019-07-03T21:14:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahulkothanath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahulkothanath/32/99122_2.png) [@rahulkothanath](https://discuss.elastic.co/u/rahulkothanath)\
**Post date:** [July 3, 2019, 9:14pm UTC](https://discuss.elastic.co/t/how-to-define-patterns-dir-as-global-in-logstash/188812/1 "2019-07-03T21:14:36Z")

</div>

Hi,  
I have a pattens file which contains diffrent patterns as line by line. I am matching each pattern as follows.

```
 if [tran_type_cd] in ["EMD","EMA","EMC","EMF","EMU","EMX"] {  
        	grok {patterns_dir => ["logstash-7.0.0/schemas/nonrtd/patterns"]
              	  match => { "message" => "%{EMD}" }}}

          else if [tran_type_cd] in ["ISG","IPV"]{
          	grok {patterns_dir => ["logstash-7.0.0/schemas/nonrtd/patterns"]
                 match => { "message" => "%{ISG}" }}}

          else if [tran_type_cd] in ["IEN","IER","MEN","MER","TEN"]{
                grok {patterns_dir => ["logstash-7.0.0/schemas/nonrtd/patterns"]
                 match => { "message" => "%{IEN}" }}}

          else if [tran_type_cd] == "IPU"{
                grok {patterns_dir => ["logstash-7.0.0/schemas/nonrtd/patterns"]
                 match => { "message" => "%{IPU}" }}}

```

I am loading patterns\_dir for each else conditions. How can I define patterns\_dir as global and match pattern directly. this could avoid defining patterns\_dir for each else if conditions.

thanks for reading and hellping.

---

<div class="post-metadata">

**Author:** ![AquaX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aquax/32/92006_2.png) [@AquaX](https://discuss.elastic.co/u/AquaX)\
**Post date:** [July 6, 2019, 9:43pm UTC](https://discuss.elastic.co/t/how-to-define-patterns-dir-as-global-in-logstash/188812/2 "2019-07-06T21:43:52Z")

</div>

You can define multiple grok patterns in a single match 🙂

```
grok {
    patterns_dir => ["logstash-7.0.0/schemas/nonrtd/patterns"]
    match => { "message" => [ "%{EMD}", 
                              "%{ISG}", 
                              "%{IEN}", 
                              "%{IPU}" ]
    }
```

---

<div class="post-metadata">

**Author:** ![rahulkothanath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahulkothanath/32/99122_2.png) [@rahulkothanath](https://discuss.elastic.co/u/rahulkothanath)\
**Post date:** [July 7, 2019, 4:29pm UTC](https://discuss.elastic.co/t/how-to-define-patterns-dir-as-global-in-logstash/188812/3 "2019-07-07T16:29:46Z")

</div>

This is useful ,but I also need to check if condition based on variable "trans\_type\_cd" before matching.

thanks for reading and helping.

---

<div class="post-metadata">

**Author:** ![AquaX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aquax/32/92006_2.png) [@AquaX](https://discuss.elastic.co/u/AquaX)\
**Post date:** [July 7, 2019, 9:59pm UTC](https://discuss.elastic.co/t/how-to-define-patterns-dir-as-global-in-logstash/188812/4 "2019-07-07T21:59:45Z")

</div>

Why do you need to check for that field before matching? Why not after?

---

<div class="post-metadata">

**Author:** ![rahulkothanath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahulkothanath/32/99122_2.png) [@rahulkothanath](https://discuss.elastic.co/u/rahulkothanath)\
**Post date:** [July 7, 2019, 10:02pm UTC](https://discuss.elastic.co/t/how-to-define-patterns-dir-as-global-in-logstash/188812/5 "2019-07-07T22:02:11Z")

</div>

if value of transa\_type\_cd is diffrent ,then I need to match message with diffrent pattern. where should I put the if else if conditions as per your solution ..

thanks for reading and helping

---

<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 8, 2019, 1:11pm UTC](https://discuss.elastic.co/t/how-to-define-patterns-dir-as-global-in-logstash/188812/6 "2019-07-08T13:11:24Z")

</div>

> [@rahulkothanath](#):
>
> if value of transa\_type\_cd is diffrent ,then I need to match message with diffrent pattern.

No, you do not. You can give the grok filter a list of patterns and it will match against whichever one works. Unless they are ambiguous it will do the if-else if-else if for you.

---

<div class="post-metadata">

**Author:** ![rahulkothanath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahulkothanath/32/99122_2.png) [@rahulkothanath](https://discuss.elastic.co/u/rahulkothanath)\
**Post date:** [July 8, 2019, 6:07pm UTC](https://discuss.elastic.co/t/how-to-define-patterns-dir-as-global-in-logstash/188812/7 "2019-07-08T18:07:16Z")

</div>

That is cool. will check it out.

thanks or reading and helping

---

<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 5, 2019, 6:07pm UTC](https://discuss.elastic.co/t/how-to-define-patterns-dir-as-global-in-logstash/188812/8 "2019-08-05T18:07:29Z")

</div>

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