# Kv filter parsing

**URL:** <https://discuss.elastic.co/t/kv-filter-parsing/174674>\
**Category:** Logstash\
**Created:** [March 31, 2019, 1:13pm UTC](https://discuss.elastic.co/t/kv-filter-parsing/174674 "2019-03-31T13:13:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paano](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@paano](https://discuss.elastic.co/u/paano)\
**Post date:** [March 31, 2019, 1:13pm UTC](https://discuss.elastic.co/t/kv-filter-parsing/174674/1 "2019-03-31T13:13:33Z")

</div>

Is there a way to use KV filter for parse payload patterns as below:

A(1), B(2), C(3), D(T1)

Looked into config-option `include_brackets`, `trim_key`, `trim_value`

logstash filter is as below:

```auto
filter {
kv {
    value_split => "("
    trim_key => "\"\ \(\)"
    field_split => ","
    }
}

```

rubydebug as below:

```auto
elk_logstash | {
elk_logstash | "@version" => "1",
elk_logstash | "message" => "A(1), B(2), C(3), D(T1), E(ttre)",
elk_logstash | "host" => {
elk_logstash | "name" => "3b015b1f625d"
elk_logstash | },
elk_logstash | "offset" => 0,
elk_logstash | "E" => "ttre)",
elk_logstash | "input" => {
elk_logstash | "type" => "log"
elk_logstash | },
elk_logstash | "D" => "T1)",
elk_logstash | "prospector" => {
elk_logstash | "type" => "log"
elk_logstash | },
elk_logstash | "B" => "2)",
elk_logstash | "@timestamp" => 2019-03-31T13:27:56.402Z,
elk_logstash | "A" => "1)",
elk_logstash | "C" => "3)",
elk_logstash | "source" => "/var/log/testfile",
elk_logstash | "beat" => {
elk_logstash | "version" => "6.5.2",
elk_logstash | "hostname" => "3b015b1f625d",
elk_logstash | "name" => "3b015b1f625d"
elk_logstash | },
elk_logstash | "tags" => []
elk_logstash | }

```

---

<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:** [March 31, 2019, 2:25pm UTC](https://discuss.elastic.co/t/kv-filter-parsing/174674/2 "2019-03-31T14:25:11Z")

</div>

You need to add trim\_value

```
    trim_value => "\)"
```

---

<div class="post-metadata">

**Author:** ![paano](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@paano](https://discuss.elastic.co/u/paano)\
**Post date:** [March 31, 2019, 2:43pm UTC](https://discuss.elastic.co/t/kv-filter-parsing/174674/3 "2019-03-31T14:43:13Z")

</div>

@Badger thanks. I was able to figure out.

Some of the value contain `,` , and with `field_split` it causes a problem. Is there a way to condition it.  
Example:  
A(1), B(2), C(3), D(1,2), E(), F(12)

---

<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:** [March 31, 2019, 4:52pm UTC](https://discuss.elastic.co/t/kv-filter-parsing/174674/4 "2019-03-31T16:52:05Z")

</div>

> [@paano](#):
>
> A(1), B(2), C(3), D(1,2), E(), F(12)

You could try

```
mutate { gsub => ["message", "(\([^()]*),(([^()]*))", "\1|\2" ] }

```

which, if it finds a , inside () changes it to |, so you end up with

```
   "message" => "A(1), B(2), C(3), D(1|2), E(), F(12)"

```

which can be parsed using

```
    kv {
        field_split => ","
        value_split => "("
        trim_key => "\"\ \(\)"
        trim_value => "\)"
    }

```

---

<div class="post-metadata">

**Author:** ![paano](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@paano](https://discuss.elastic.co/u/paano)\
**Post date:** [March 31, 2019, 6:06pm UTC](https://discuss.elastic.co/t/kv-filter-parsing/174674/5 "2019-03-31T18:06:28Z")

</div>

Yep, worked. Added gusb again to revert the `|` to `,` after the `kv` filter. Thanks.

---

<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:** [April 28, 2019, 6:06pm UTC](https://discuss.elastic.co/t/kv-filter-parsing/174674/6 "2019-04-28T18:06:28Z")

</div>

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