# Logstash Create new field from existing field and remove character from new field

**URL:** <https://discuss.elastic.co/t/logstash-create-new-field-from-existing-field-and-remove-character-from-new-field/277268>\
**Category:** Logstash\
**Created:** [June 28, 2021, 7:52pm UTC](https://discuss.elastic.co/t/logstash-create-new-field-from-existing-field-and-remove-character-from-new-field/277268 "2021-06-28T19:52:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Annette1](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@Annette1](https://discuss.elastic.co/u/Annette1)\
**Post date:** [June 28, 2021, 7:52pm UTC](https://discuss.elastic.co/t/logstash-create-new-field-from-existing-field-and-remove-character-from-new-field/277268/1 "2021-06-28T19:52:25Z")

</div>

I have a simple request but seem unable to be able execute it correctly.

I have a field named logname that has several name forms. For instance are logname-app, logname-os, logname-access, and logname. The logname is variable. The goal is to produce a variable logname in the path without the ending -app, -acess -os, etc and to use this in a logstash prefix. For instance, I would like to end with:  
prefix =\> "%{+YYYY]/%{+MM}/%{+dd}/%{[sublog]}/application  
prefix =\> "%{+YYYY]/%{+MM}/%{+dd}/%{[sublog]}/os  
prefix =\> "%{+YYYY]/%{+MM}/%{+dd}/%{[sublog]}/access...etc.

I have the if statement working correctly based on the presence of the end of the field for instance.... if "-app" in [logname]....then moved data to and produce the one of the varients of the prefix above....

I most recently tried

filter {  
if "-app" in [logname] [  
mutate {  
add field =\> {  
"[sublog] =\> "%{[logname]}" }  
remove\_field =\> "-app"

the above does not work.

What I cannot get working is the filter or whatever to not only create the new sublog from the [logname], but remove the -app, etc from the newly formed sublog field so I can correctly insert the new sublog into the prefix without the -app at the end . Any help would be appreciated

---

<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:** [June 28, 2021, 8:25pm UTC](https://discuss.elastic.co/t/logstash-create-new-field-from-existing-field-and-remove-character-from-new-field/277268/2 "2021-06-28T20:25:46Z")

</div>

Can you show using JSON a couple of examples of what your data looks like?

---

<div class="post-metadata">

**Author:** ![Annette1](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@Annette1](https://discuss.elastic.co/u/Annette1)\
**Post date:** [June 28, 2021, 8:34pm UTC](https://discuss.elastic.co/t/logstash-create-new-field-from-existing-field-and-remove-character-from-new-field/277268/3 "2021-06-28T20:34:48Z")

</div>

application1-app, application1-os, application1-access, application1-startup  
application2-app, application2-os, application2-access, application2-startup  
application3-app, application3-os, application3-access, application3-startup

I just want the application1, application2, application3 part of the field to show up in the prefix.

---

<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:** [June 28, 2021, 8:56pm UTC](https://discuss.elastic.co/t/logstash-create-new-field-from-existing-field-and-remove-character-from-new-field/277268/4 "2021-06-28T20:56:42Z")

</div>

Assuming that that shows three separate events, and that they are the names of the fields then you would have to use ruby. I have not tested it but something like

```
ruby {
    code => '
        event.to_hash.each do |k, v|
            if k =~ /-app$/
                event.set("[@metadata][app]", k)
                break
        end
    '
}
grok { match => { "[@metadata][app]" => "^%{GREEDYDATA:[@metadata][prefix]}-app$" } }

```

[@metadata][prefix] will then contain "application1" or "application2", etc. You could then use

```
prefix => "%{+YYYY]/%{+MM}/%{+dd}/%{[@metadata][prefix]}/application

```

fields under [@metadata] are available in logstash, but outputs do not use them, so they are handy for storing data only needed inside the pipeline.

---

<div class="post-metadata">

**Author:** ![Annette1](https://avatars.discourse-cdn.com/v4/letter/a/ba9def/32.png) [@Annette1](https://discuss.elastic.co/u/Annette1)\
**Post date:** [June 28, 2021, 9:16pm UTC](https://discuss.elastic.co/t/logstash-create-new-field-from-existing-field-and-remove-character-from-new-field/277268/5 "2021-06-28T21:16:45Z")

</div>

thanks...digesting this

---

<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:** [July 26, 2021, 9:17pm UTC](https://discuss.elastic.co/t/logstash-create-new-field-from-existing-field-and-remove-character-from-new-field/277268/6 "2021-07-26T21:17:20Z")

</div>

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