# Logstash filter for renaming based on a substring

**URL:** <https://discuss.elastic.co/t/logstash-filter-for-renaming-based-on-a-substring/165089>\
**Category:** Logstash\
**Created:** [January 21, 2019, 4:40pm UTC](https://discuss.elastic.co/t/logstash-filter-for-renaming-based-on-a-substring/165089 "2019-01-21T16:40:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomasz](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@tomasz](https://discuss.elastic.co/u/tomasz)\
**Post date:** [January 21, 2019, 4:40pm UTC](https://discuss.elastic.co/t/logstash-filter-for-renaming-based-on-a-substring/165089/1 "2019-01-21T16:40:21Z")

</div>

I can't figure out how to rename fileds based on the pattern.

At the moment, in Logstash (6.5) I have a banch of filters:

```
mutate { rename => { "sensor_source_a" => "sensor.labels.a" } }
mutate { rename => { "sensor_source_b" => "sensor.labels.b" } }
mutate { rename => { "sensor_source_c" => "sensor.labels.c" } }

```

I need to make some more automated filter which independently of the last element will always rename the pattern:  
"sensor\_source\_xxx" into "sensor.labels.xxx"  
I am trying different approaches together with ruby code but can't find simple solution.  
What would be the most siutable filter for such opearation

---

<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:** [January 21, 2019, 5:23pm UTC](https://discuss.elastic.co/t/logstash-filter-for-renaming-based-on-a-substring/165089/2 "2019-01-21T17:23:37Z")

</div>

Something like:

```
ruby {
    code => '
        event.to_hash.each_pair { |k, v|
            k.match(/^sensor_source_(.*)/) { |m|
                i = "[sensor][labels][#{m[1][0]}]"
                event.set(i, v)
                event.remove(k)
            }
        }
    '
}
```

---

<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:** [February 18, 2019, 5:23pm UTC](https://discuss.elastic.co/t/logstash-filter-for-renaming-based-on-a-substring/165089/3 "2019-02-18T17:23:38Z")

</div>

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