# Remove first and last character of all fields

**URL:** <https://discuss.elastic.co/t/remove-first-and-last-character-of-all-fields/222897>\
**Category:** Logstash\
**Created:** [March 10, 2020, 9:42am UTC](https://discuss.elastic.co/t/remove-first-and-last-character-of-all-fields/222897 "2020-03-10T09:42:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vertigo888](https://avatars.discourse-cdn.com/v4/letter/v/5fc32e/32.png) [@vertigo888](https://discuss.elastic.co/u/vertigo888)\
**Post date:** [March 10, 2020, 9:42am UTC](https://discuss.elastic.co/t/remove-first-and-last-character-of-all-fields/222897/1 "2020-03-10T09:42:21Z")

</div>

Hi,

I would like to replace the first and last character of all values in all fields ingested into my index via logstash? Please could you let me know how this is possible?

I understand this may be a ruby task but please let me know.

Thanks

---

<div class="post-metadata">

**Author:** ![vertigo888](https://avatars.discourse-cdn.com/v4/letter/v/5fc32e/32.png) [@vertigo888](https://discuss.elastic.co/u/vertigo888)\
**Post date:** [March 10, 2020, 10:54am UTC](https://discuss.elastic.co/t/remove-first-and-last-character-of-all-fields/222897/2 "2020-03-10T10:54:50Z")

</div>

I did try the below code too but does not seem to work I get a ruby exception occurred undefined method `' for nil:NilClass:

> ruby {  
> code =\> "  
> hash = event.to\_hash  
> hash.each { |k,v|  
> event.set(k, event.get(v)[1..-1])  
> }  
> "  
> }

---

<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 10, 2020, 2:13pm UTC](https://discuss.elastic.co/t/remove-first-and-last-character-of-all-fields/222897/3 "2020-03-10T14:13:27Z")

</div>

> [@vertigo888](#):
>
> event.get(v)

I don't think you need the event.get, you can just use v to refer to the value of a field.

---

<div class="post-metadata">

**Author:** ![vertigo888](https://avatars.discourse-cdn.com/v4/letter/v/5fc32e/32.png) [@vertigo888](https://discuss.elastic.co/u/vertigo888)\
**Post date:** [March 10, 2020, 4:05pm UTC](https://discuss.elastic.co/t/remove-first-and-last-character-of-all-fields/222897/4 "2020-03-10T16:05:18Z")

</div>

Using event.set(k, v[1..-1]) instead but i get undefined method ` ' for logstash::timestamp

Any ideas?

---

<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 10, 2020, 5:07pm UTC](https://discuss.elastic.co/t/remove-first-and-last-character-of-all-fields/222897/5 "2020-03-10T17:07:41Z")

</div>

Yeah, that seems reasonable, since you cannot refer to a LogStash::Timestamp as an array. If you want to remove the first and last characters of every string field you could try

```
code => '
    event.to_hash.each { |k, v|
        if v.is_a? String
            event.set(k, v[1,-2])
        end
    }
'
```

---

<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 7, 2020, 5:07pm UTC](https://discuss.elastic.co/t/remove-first-and-last-character-of-all-fields/222897/6 "2020-04-07T17:07:56Z")

</div>

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