# Add new fields based on hostname substring

**URL:** <https://discuss.elastic.co/t/add-new-fields-based-on-hostname-substring/327845>\
**Category:** Logstash\
**Created:** [March 16, 2023, 10:43am UTC](https://discuss.elastic.co/t/add-new-fields-based-on-hostname-substring/327845 "2023-03-16T10:43:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lemospt](https://avatars.discourse-cdn.com/v4/letter/l/e47c2d/32.png) [@lemospt](https://discuss.elastic.co/u/lemospt)\
**Post date:** [March 16, 2023, 10:43am UTC](https://discuss.elastic.co/t/add-new-fields-based-on-hostname-substring/327845/1 "2023-03-16T10:43:13Z")

</div>

Hi guys,

i want to add new fields based on the information in hostname. Below the examples of what i need,

hostname=alfrdnsresolverfixed01  
new fields: site=alfr, dns\_type=fixed

hostname=boavdnsresolvermbbnat01  
new fields: site=boav, dns\_type=mbbnat

so site is the first 4 chars of hostname and dns\_type is between dnsresolver and last 2 digits

thanks,

---

<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 16, 2023, 3:08pm UTC](https://discuss.elastic.co/t/add-new-fields-based-on-hostname-substring/327845/2 "2023-03-16T15:08:42Z")

</div>

Use grok

```
grok { match => { "hostname" => "^%{WORD:site}dnsresolver%{WORD:dns_type}\d" } }

```

---

<div class="post-metadata">

**Author:** ![lemospt](https://avatars.discourse-cdn.com/v4/letter/l/e47c2d/32.png) [@lemospt](https://discuss.elastic.co/u/lemospt)\
**Post date:** [March 16, 2023, 3:50pm UTC](https://discuss.elastic.co/t/add-new-fields-based-on-hostname-substring/327845/3 "2023-03-16T15:50:21Z")

</div>

more simple than i thought.

@Badger \ d in the end what means? 1 digit, 1 or more digits, something else...?

---

<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 16, 2023, 4:05pm UTC](https://discuss.elastic.co/t/add-new-fields-based-on-hostname-substring/327845/4 "2023-03-16T16:05:26Z")

</div>

\d means one digit. That may be followed by another digit or anything else. There is no need to match the entire field.

---

<div class="post-metadata">

**Author:** ![lemospt](https://avatars.discourse-cdn.com/v4/letter/l/e47c2d/32.png) [@lemospt](https://discuss.elastic.co/u/lemospt)\
**Post date:** [March 16, 2023, 7:41pm UTC](https://discuss.elastic.co/t/add-new-fields-based-on-hostname-substring/327845/5 "2023-03-16T19:41:53Z")

</div>

need to adjust things but work like a charm.

many thanks @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 13, 2023, 7:42pm UTC](https://discuss.elastic.co/t/add-new-fields-based-on-hostname-substring/327845/6 "2023-04-13T19:42:30Z")

</div>

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