# Extract a substring and assign to new field

**URL:** https://discuss.elastic.co/t/extract-a-substring-and-assign-to-new-field/284692
**Category:** Logstash
**Created:** [September 21, 2021, 8:37am UTC](https://discuss.elastic.co/t/extract-a-substring-and-assign-to-new-field/284692 "2021-09-21T08:37:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Andrea\_Bozzano](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_bozzano/32/73731_2.png) [@Andrea\_Bozzano](https://discuss.elastic.co/u/Andrea_Bozzano)
#### Post date: [September 21, 2021, 8:37am UTC](https://discuss.elastic.co/t/extract-a-substring-and-assign-to-new-field/284692/1 "2021-09-21T08:37:33Z")

</div>

Hi everyone!  
Hope everyone's good.

in my application, i'm sending a JSON wich has a string field always made of 16 char  
For example: XXXYYY12Y00K111H

Is there a fine way, or do you have any particular hint to extract, for example, the number 12, and assign it to a new field?

Thank you for any reply!

---

<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: [September 21, 2021, 2:24pm UTC](https://discuss.elastic.co/t/extract-a-substring-and-assign-to-new-field/284692/2 "2021-09-21T14:24:30Z")

</div>

You can use grok

```
grok { match => { "someField" => "^.{6}(?<anotherField>..)" } }

```

will extract the seventh and eighth characters from a string.

---

<div class="post-metadata">

### Author: ![Andrea\_Bozzano](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrea_bozzano/32/73731_2.png) [@Andrea\_Bozzano](https://discuss.elastic.co/u/Andrea_Bozzano)
#### Post date: [September 21, 2021, 3:10pm UTC](https://discuss.elastic.co/t/extract-a-substring-and-assign-to-new-field/284692/3 "2021-09-21T15:10:18Z")

</div>

Thanks!

This actually worked quite good, and with some adaptation also for other things 🙂

---

<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: [October 19, 2021, 3:10pm UTC](https://discuss.elastic.co/t/extract-a-substring-and-assign-to-new-field/284692/4 "2021-10-19T15:10:48Z")

</div>

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