# Logstash Grok - Extract field if field exists & deleting character

**URL:** <https://discuss.elastic.co/t/logstash-grok-extract-field-if-field-exists-deleting-character/279492>\
**Category:** Logstash\
**Created:** [July 23, 2021, 2:31pm UTC](https://discuss.elastic.co/t/logstash-grok-extract-field-if-field-exists-deleting-character/279492 "2021-07-23T14:31:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gringorion](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@gringorion](https://discuss.elastic.co/u/gringorion)\
**Post date:** [July 23, 2021, 2:31pm UTC](https://discuss.elastic.co/t/logstash-grok-extract-field-if-field-exists-deleting-character/279492/1 "2021-07-23T14:31:43Z")

</div>

Hello Guys,

My case is simple, but can't find the perfect response.

I need to extract the field: `user@domain` into 2 fields.  
This step is OK:  
`%{WORD:user}%{NOTSPACE:domain}?`  
gives:

```auto
{
  "user": "user",
  "domain": "@domain"
}

```

This domain field is not present all the time, so I may have only user, this is why I have the ? character:

so the field: `user`  
gives

```auto
{
  "user": "user"
}

```

with the same Grok Pattern

What I don't succeed to do, is to delete the @ in domain field when this field is found, using the same pattern in both cases.

Do you have any idea ?

Many 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:** [July 23, 2021, 5:24pm UTC](https://discuss.elastic.co/t/logstash-grok-extract-field-if-field-exists-deleting-character/279492/2 "2021-07-23T17:24:45Z")

</div>

You could try

```
%{WORD:user}@?%{NOTSPACE:domain}?
```

---

<div class="post-metadata">

**Author:** ![gringorion](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@gringorion](https://discuss.elastic.co/u/gringorion)\
**Post date:** [July 23, 2021, 7:13pm UTC](https://discuss.elastic.co/t/logstash-grok-extract-field-if-field-exists-deleting-character/279492/3 "2021-07-23T19:13:38Z")

</div>

I knew it was a great idea to post here.  
Badger, I think I owe you a beer!

thanks a lot !

---

<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:** [August 20, 2021, 7:14pm UTC](https://discuss.elastic.co/t/logstash-grok-extract-field-if-field-exists-deleting-character/279492/4 "2021-08-20T19:14:08Z")

</div>

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