# Filter first match only using Grok or ruby code

**URL:** <https://discuss.elastic.co/t/filter-first-match-only-using-grok-or-ruby-code/315758>\
**Category:** Logstash\
**Created:** [October 4, 2022, 10:21am UTC](https://discuss.elastic.co/t/filter-first-match-only-using-grok-or-ruby-code/315758 "2022-10-04T10:21:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rootk1d](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rootk1d/32/108615_2.png) [@rootk1d](https://discuss.elastic.co/u/rootk1d)\
**Post date:** [October 4, 2022, 10:21am UTC](https://discuss.elastic.co/t/filter-first-match-only-using-grok-or-ruby-code/315758/1 "2022-10-04T10:21:02Z")

</div>

I have a field that contains the following data

```auto
"REQUEST-941-APPLICATION-ATTACK-XSS, REQUEST-941-APPLICATION-ATTACK-XSS, REQUEST-941-APPLICATION-ATTACK-XSS, REQUEST-942-APPLICATION-ATTACK-SQLI, REQUEST-949-BLOCKING-EVALUATION, RESPONSE-980-CORRELATION

```

And I want to return the first item only ie: (REQUEST-941-APPLICATION-ATTACK-XSS), but when i use this grok filter

```auto
grok
    {
        match => {"attack_names" => "(?<attack>^[A-Z1-9-]+)"}
        remove_field => ["attack_names"]
    }

```

it return all occurrences

```auto
"REQUEST-941-APPLICATION-ATTACK-XSS, REQUEST-941-APPLICATION-ATTACK-XSS, REQUEST-941-APPLICATION-ATTACK-XSS, REQUEST-942-APPLICATION-ATTACK-SQLI, REQUEST-949-BLOCKING-EVALUATION, RESPONSE-980-CORRELATION

```

So, I tried this ruby code:

```auto
ruby 
    {
 	code => "
    		saveid = event.get('attack_names').split(',')[0]
    		event.set('attack',saveid)
    		"
    }

```

but this dosen't seem to do anything and the field isn't added to the data.

So, what I'm doing wrong here?

---

<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:** [October 4, 2022, 11:52am UTC](https://discuss.elastic.co/t/filter-first-match-only-using-grok-or-ruby-code/315758/2 "2022-10-04T11:52:44Z")

</div>

1. Grok

```auto
grok
    {
        match => {"attack_names" => "%{USERNAME:attack},\s+"}
        remove_field => ["attack_names"]
    }

```

1. Use csv plugin on attack\_names, rename only column1
2. Use mutate gsub to split by "," , your array will have 1st member [0]

---

<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:** [November 1, 2022, 11:53am UTC](https://discuss.elastic.co/t/filter-first-match-only-using-grok-or-ruby-code/315758/3 "2022-11-01T11:53:08Z")

</div>

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