# Cutting or parsing specific characters out of a filtered variable

**URL:** <https://discuss.elastic.co/t/cutting-or-parsing-specific-characters-out-of-a-filtered-variable/196400>\
**Category:** Logstash\
**Created:** [August 22, 2019, 8:10pm UTC](https://discuss.elastic.co/t/cutting-or-parsing-specific-characters-out-of-a-filtered-variable/196400 "2019-08-22T20:10:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/charles_badger/32/52849_2.png) [@Charles\_Badger](https://discuss.elastic.co/u/Charles_Badger)\
**Post date:** [August 22, 2019, 8:10pm UTC](https://discuss.elastic.co/t/cutting-or-parsing-specific-characters-out-of-a-filtered-variable/196400/1 "2019-08-22T20:10:16Z")

</div>

I am dealing with a filter where it pulls out sudo commands from RHEL 6 OS.

An example is this: "argc=2 a0=sudo a1=chpasswd"  
I currently in my grok filter use this command to assign it to a variable: ".\*argc=%{DATA:CMD}\d{2}"  
results in: "2 a0=sudo a1=chpasswd"

My question is how can I still get that data filtered for my variable CMD, but remove the "2 a0=" and the "a1=" so that the results for CMD ends up being: "sudo chpasswd"?

Thanks for the help!

---

<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:** [August 22, 2019, 9:50pm UTC](https://discuss.elastic.co/t/cutting-or-parsing-specific-characters-out-of-a-filtered-variable/196400/2 "2019-08-22T21:50:23Z")

</div>

I apologize for the awful ugliness of this code, but

```
    kv { source => "message" target => "[@metadata][kvdata]" }
    ruby {
        code => '
            kvdata = event.get("[@metadata][kvdata]")
            i = 0
            s = ""
            while v = kvdata["a#{i}"]
                s += v + " "
                i += 1
            end
            event.set("someField", s.chomp(" "))
        '
    }
```

---

<div class="post-metadata">

**Author:** ![Charles\_Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/charles_badger/32/52849_2.png) [@Charles\_Badger](https://discuss.elastic.co/u/Charles_Badger)\
**Post date:** [September 11, 2019, 8:03pm UTC](https://discuss.elastic.co/t/cutting-or-parsing-specific-characters-out-of-a-filtered-variable/196400/3 "2019-09-11T20:03:07Z")

</div>

Is there a way to do it without looping?

---

<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:** [October 9, 2019, 8:03pm UTC](https://discuss.elastic.co/t/cutting-or-parsing-specific-characters-out-of-a-filtered-variable/196400/4 "2019-10-09T20:03:08Z")

</div>

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