# Grok field extract: One pattern, multiple matches

**URL:** <https://discuss.elastic.co/t/grok-field-extract-one-pattern-multiple-matches/130497>\
**Category:** Logstash\
**Created:** [May 3, 2018, 4:43pm UTC](https://discuss.elastic.co/t/grok-field-extract-one-pattern-multiple-matches/130497 "2018-05-03T16:43:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonrust](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jonrust](https://discuss.elastic.co/u/jonrust)\
**Post date:** [May 3, 2018, 4:43pm UTC](https://discuss.elastic.co/t/grok-field-extract-one-pattern-multiple-matches/130497/1 "2018-05-03T16:43:47Z")

</div>

Oracle alert logs contain multiple ORA- codes. I can't figure how to tell grok to match all and return an array of the matches.

```
 if [msg] =~ /ORA-/ {
    grok {
       match => ["msg","(?<ora_code>ORA-[0-9]+)" ]
    }
 }

```

Log example might have:

> ORA-12345: some message ORA-45678: some other message

Is it possible?

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [May 3, 2018, 4:49pm UTC](https://discuss.elastic.co/t/grok-field-extract-one-pattern-multiple-matches/130497/2 "2018-05-03T16:49:56Z")

</div>

You're looking for [`break_on_match => false`](https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html#plugins-filters-grok-break_on_match) 🙂

---

<div class="post-metadata">

**Author:** ![jonrust](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jonrust](https://discuss.elastic.co/u/jonrust)\
**Post date:** [May 3, 2018, 4:51pm UTC](https://discuss.elastic.co/t/grok-field-extract-one-pattern-multiple-matches/130497/3 "2018-05-03T16:51:18Z")

</div>

That didn't do the trick. Tried this:

```
 if [msg] =~ /ORA-/ {
    grok {
       break_on_match => false
       match => ["msg","(?<ora_code>ORA-[0-9]+)" ]
    }
 }
```

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [May 3, 2018, 5:13pm UTC](https://discuss.elastic.co/t/grok-field-extract-one-pattern-multiple-matches/130497/4 "2018-05-03T17:13:43Z")

</div>

> [@jonrust](#):
>
> ORA-12345: some message ORA-45678: some other message

d'oh! that flag will keep looking at _patterns_ but not at the input after a match. I think I can whip up something quick with a ruby script for you though.

---

<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:** [May 3, 2018, 5:13pm UTC](https://discuss.elastic.co/t/grok-field-extract-one-pattern-multiple-matches/130497/5 "2018-05-03T17:13:46Z")

</div>

Use ruby.

```auto
  code => "
    s = event.get('message')
    r = s.scan(/(ORA-[0-9]{5})/)
    r = r.flatten
    event.set('OracleErrors', r.join('/'))
  "

```

Actually, if you want an array, do not do the join, just set OracleErrors to r.

---

<div class="post-metadata">

**Author:** ![jonrust](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jonrust](https://discuss.elastic.co/u/jonrust)\
**Post date:** [May 3, 2018, 5:36pm UTC](https://discuss.elastic.co/t/grok-field-extract-one-pattern-multiple-matches/130497/6 "2018-05-03T17:36:38Z")

</div>

That did the trick. Thanks.

```
  if [msg] =~ /ORA-/ {
     ruby {
        code => "
           mat = event.get('msg').scan(/(ORA-[0-9]+)/)
           event.set('ora_code', mat.flatten)
        "
     }
```

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [May 3, 2018, 6:11pm UTC](https://discuss.elastic.co/t/grok-field-extract-one-pattern-multiple-matches/130497/7 "2018-05-03T18:11:46Z")

</div>

I whipped up a quick ruby filter with tests that capture edge-cases (such as the event not having a value at `msg`, or the string containing no matches):

> <https://gist.github.com/yaauie/7e994b10ff538e18c3a363645fedf137>

---

<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:** [May 31, 2018, 6:11pm UTC](https://discuss.elastic.co/t/grok-field-extract-one-pattern-multiple-matches/130497/8 "2018-05-31T18:11:52Z")

</div>

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