# Extract From a Field Name and Add Fields

**URL:** <https://discuss.elastic.co/t/extract-from-a-field-name-and-add-fields/216911>\
**Category:** Logstash\
**Created:** [January 28, 2020, 8:55pm UTC](https://discuss.elastic.co/t/extract-from-a-field-name-and-add-fields/216911 "2020-01-28T20:55:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfisher](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mfisher](https://discuss.elastic.co/u/mfisher)\
**Post date:** [January 28, 2020, 8:55pm UTC](https://discuss.elastic.co/t/extract-from-a-field-name-and-add-fields/216911/1 "2020-01-28T20:55:32Z")

</div>

I need to copy the dynamic " **MODEL**" from the field name and copy the value " **RandomSerial**" to new fields.

When " **MODEL**" and " **RandomSerial**" will be different for all messages.

Logstash Output:

> "Serial Number ( **MODEL** )" =\> " **RandomSerial**"

To this:

> "Model" =\> " **MODEL**"  
> "Serial Number" =\> " **RandomSerial**"

My thought is this would require ruby code which I'm not familiar with.

Thanks!

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [January 28, 2020, 9:44pm UTC](https://discuss.elastic.co/t/extract-from-a-field-name-and-add-fields/216911/2 "2020-01-28T21:44:22Z")

</div>

I didn't understand question.  
where is model comes from?

if your output is  
"Serial Number ( **MODEL** )" =\> " **RandomSerial**"  
that means field = serial number(model)  
and value of that is RandomSerial.

where is this value for Model is coming from?

This is how I am seeing your post  
Field =\> value

---

<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 28, 2020, 10:06pm UTC](https://discuss.elastic.co/t/extract-from-a-field-name-and-add-fields/216911/3 "2020-01-28T22:06:05Z")

</div>

You could do that with either dissect or grok, it should not require ruby.

---

<div class="post-metadata">

**Author:** ![mfisher](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mfisher](https://discuss.elastic.co/u/mfisher)\
**Post date:** [January 28, 2020, 11:50pm UTC](https://discuss.elastic.co/t/extract-from-a-field-name-and-add-fields/216911/4 "2020-01-28T23:50:50Z")

</div>

The fields and values are being populated with a ruby script from nested JSON. This thread shows all the configs associated. [Utilize Data Parsed From Nested JSON](https://discuss.elastic.co/t/utilize-data-parsed-from-nested-json/216339)

---

<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 29, 2020, 12:45am UTC](https://discuss.elastic.co/t/extract-from-a-field-name-and-add-fields/216911/5 "2020-01-29T00:45:46Z")

</div>

OK, I think I misunderstood. You have a field on the event with a variable field name "Serial Number ( MODEL )" and its value is "RandomSerial"? You want to rename the field to a fixed value and also extract the variable part of the field name as the value of a field called [Model]? That will definitely require ruby.

I don't have time to test this but you could start with something like:

```
code => '
    event.to_hash.each { |k, v|
        if k.start_with?("Serial Number (")
             event.set("Model", k.gsub("[^(]+\(", "").gsub(")$", ""))
             event.set("SerialNumber", v)
        end
    }
'

```

Apologies if my ruby code (like the double gsub) makes your eyeballs bleed.

I recommend against using space in field names (hence [SerialNumber] instead of [Serial Number]).

---

<div class="post-metadata">

**Author:** ![mfisher](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mfisher](https://discuss.elastic.co/u/mfisher)\
**Post date:** [January 29, 2020, 5:53pm UTC](https://discuss.elastic.co/t/extract-from-a-field-name-and-add-fields/216911/6 "2020-01-29T17:53:19Z")

</div>

That worked as is, thanks for your Ruby help again!

I have a question about the other code you supplied me in the other thread.

---

<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 26, 2020, 5:53pm UTC](https://discuss.elastic.co/t/extract-from-a-field-name-and-add-fields/216911/7 "2020-02-26T17:53:20Z")

</div>

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