# Capitalize Every Word In String

**URL:** https://discuss.elastic.co/t/capitalize-every-word-in-string/266997
**Category:** Logstash
**Created:** [March 11, 2021, 7:58pm UTC](https://discuss.elastic.co/t/capitalize-every-word-in-string/266997 "2021-03-11T19:58:43Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [March 11, 2021, 7:58pm UTC](https://discuss.elastic.co/t/capitalize-every-word-in-string/266997/1 "2021-03-11T19:58:43Z")

</div>

Trying to capitalize each word in a fields value. I initially tried the titleize function of Ruby, but that didn't work...I guess because it's not part of Ruby internals or something like that?

```auto
  ruby {
    code => 'event.set("[Group]", event.get("[Group]").titleize)'
  }

```

Anybody got another way to do this?

---

<div class="post-metadata">

### Author: ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)
#### Post date: [March 11, 2021, 8:58pm UTC](https://discuss.elastic.co/t/capitalize-every-word-in-string/266997/2 "2021-03-11T20:58:51Z")

</div>

There is a mutate filter for all operations like : _uppercase_ ; _capitalize_ ; _lowercase_

> **[Mutate filter plugin | Logstash Reference \[master\] | Elastic](https://www.elastic.co/guide/en/logstash/master/plugins-filters-mutate.html#plugins-filters-mutate-capitalize)**

---

<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 11, 2021, 9:16pm UTC](https://discuss.elastic.co/t/capitalize-every-word-in-string/266997/3 "2021-03-11T21:16:33Z")

</div>

> [@wwalker](#):
>
> I guess because it's not part of Ruby internals or something like that?

event.get will return a Ruby string if the field is a string. titleize is part of Ruby on Rails, not core Ruby.

---

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [March 11, 2021, 9:20pm UTC](https://discuss.elastic.co/t/capitalize-every-word-in-string/266997/4 "2021-03-11T21:20:42Z")

</div>

> [@ylasri](#):
>
> There is a mutate filter for all operations like : _uppercase_ ; _capitalize_ ; _lowercase_

None of those functions accomplish my needs.

Raw Value: jOhN dOe  
Desired Value: John Doe

Uppercase: JOHN DOE  
Lowercase: john doe  
Capitalize: JOhN DOe

---

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [March 11, 2021, 9:21pm UTC](https://discuss.elastic.co/t/capitalize-every-word-in-string/266997/5 "2021-03-11T21:21:42Z")

</div>

> [@Badger](#):
>
> > [@wwalker](#):
> >
> > I guess because it's not part of Ruby internals or something like that?
> 
> event.get will return a Ruby string if the field is a string. titleize is part of Ruby on Rails, not core Ruby.

Does the Ruby filter implement Ruby on Rails or core Ruby? I guess core since the code doesn't work, or is my ruby code above incorrect?

---

<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 11, 2021, 10:31pm UTC](https://discuss.elastic.co/t/capitalize-every-word-in-string/266997/6 "2021-03-11T22:31:38Z")

</div>

The ruby filter does not implement Ruby on Rails, just core Ruby.

You can lowercase and then capitalize. A ruby filter does operations in the order uppercase, capitalize, lowercase. So you will need two filters

```
mutate { lowercase => ["Group"] }
mutate { capitalize=> ["Group"] }
```

---

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [March 11, 2021, 10:33pm UTC](https://discuss.elastic.co/t/capitalize-every-word-in-string/266997/7 "2021-03-11T22:33:14Z")

</div>

This is what I tried first, but it just capitalized the first word.

---

<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 11, 2021, 10:53pm UTC](https://discuss.elastic.co/t/capitalize-every-word-in-string/266997/8 "2021-03-11T22:53:45Z")

</div>

Ah, yes. The [filter](https://github.com/logstash-plugins/logstash-filter-mutate/blob/883c8b007c41e2c8cf0107447e3de87baee7e89e/lib/logstash/filters/mutate.rb#L467) is calling the ruby .capitalize method on the entire field, which lowercases everything and then capitalizes the first character (so the first mutate is not needed). What we need to do is chop the field up into words, capitalize each one, then put them back together.

```
ruby { code => 'event.set("Group", event.get("Group").split.map(&:capitalize).join(" "))' }
```

---

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [March 12, 2021, 3:56pm UTC](https://discuss.elastic.co/t/capitalize-every-word-in-string/266997/9 "2021-03-12T15:56:27Z")

</div>

That did it. As always, thanks for the help @Badger

---

<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 9, 2021, 3:57pm UTC](https://discuss.elastic.co/t/capitalize-every-word-in-string/266997/10 "2021-04-09T15:57:21Z")

</div>

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