# Making a grok filed optional in grok pattern

**URL:** <https://discuss.elastic.co/t/making-a-grok-filed-optional-in-grok-pattern/303159>\
**Category:** Logstash\
**Created:** [April 25, 2022, 12:41pm UTC](https://discuss.elastic.co/t/making-a-grok-filed-optional-in-grok-pattern/303159 "2022-04-25T12:41:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pranjal\_Sett](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pranjal_sett/32/98834_2.png) [@Pranjal\_Sett](https://discuss.elastic.co/u/Pranjal_Sett)\
**Post date:** [April 25, 2022, 12:41pm UTC](https://discuss.elastic.co/t/making-a-grok-filed-optional-in-grok-pattern/303159/1 "2022-04-25T12:41:07Z")

</div>

Hi,

I am trying to make my field optional. Let me give you the scenario below:

let suppose my log patterns are:

pattern 1:

[2022-04-25T12:51:27.967+02:00] [d03-c02-s02] [NOTIFICATION] [oracle.wsm.msg.logging] [tid: 102] [userId:] [ecid: \*] [APP: soa-infra] [partition-name: DOMAIN] [tenant-name: GLOBAL] [WSM\_ServiceID: default/SalesTransactionEBSV2/SalesTransactionMediator] [WSM\_OperationName: \*] [FlowId: xyz] [oracle.wsm.policy.name: oracle/log\_policy] [WSM\_LogType: Request] [[XML]]

pattern 2:

[2022-04-22T13:11:14.858+02:00] [d03-c02-s02] [NOTIFICATION] [oracle.wsm.msg.logging] [tid: 137] [userId:] [ecid: \*] [APP: soa-infra] [partition-name: DOMAIN] [tenant-name: GLOBAL] [WSM\_ServiceID: \*] [FlowId: abc] [oracle.wsm.policy.name: oracle/log\_policy] [WSM\_LogType: Request] [[XML]]

pattern 3:

[2022-04-08T19:40:11.536+02:00] [d03-c02-s02] [NOTIFICATION] [oracle.wsm.msg.logging] [tid: 2729] [userId:] [ecid: \*] [APP: soa-infra] [partition-name: DOMAIN] [tenant-name: GLOBAL] [J2EE\_APP.name: soa-infra] [J2EE\_MODULE.name: fabric] [WEBSERVICE.name: \*] [WEBSERVICE\_PORT.name: \*] [WSM\_RemoteAddress: \*] [WSM\_ServiceID: \*] [WSM\_OperationName: \*] [oracle.wsm.policy.name: oracle/log\_policy] [WSM\_LogType: Response] [[XML]]

So if you noticed here upto tenant-name all of them is having same patterns but after that it was changed some times.

My question is how can I make all those fields optional after tenant-name. I have tried some of the solutions but it got fails.

tried this solution [Making a part in the grok expression optional](https://discuss.elastic.co/t/making-a-part-in-the-grok-expression-optional/43961) , but I feel my "[" this bracket creating some problem here! Not able to match grok patterns.

Thanks,  
Pranjal

---

<div class="post-metadata">

**Author:** ![Pranjal\_Sett](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pranjal_sett/32/98834_2.png) [@Pranjal\_Sett](https://discuss.elastic.co/u/Pranjal_Sett)\
**Post date:** [April 25, 2022, 12:41pm UTC](https://discuss.elastic.co/t/making-a-grok-filed-optional-in-grok-pattern/303159/2 "2022-04-25T12:41:55Z")

</div>

@Badger , Can you please help on this?

---

<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:** [April 25, 2022, 5:29pm UTC](https://discuss.elastic.co/t/making-a-grok-filed-optional-in-grok-pattern/303159/3 "2022-04-25T17:29:38Z")

</div>

> [@Pranjal\_Sett](#):
>
> [J2EE\_APP.name: soa-infra]

If that field is optional then you can use ? (zero or more) or {0,1} (zero or exactly one) to indicate that. The square brackets must be escaped to show that they are not part of a character group. Use parentheses to indicate what {0,1} applies to

```
(\[J2EE_APP.name: [-\w]+\]){0,1}

```

In some cases it is easier to [search for multiple patterns](https://discuss.elastic.co/t/parse-mcafee-audit-log-file-with-logstash/302946/2).

---

<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:** [May 23, 2022, 5:30pm UTC](https://discuss.elastic.co/t/making-a-grok-filed-optional-in-grok-pattern/303159/4 "2022-05-23T17:30:19Z")

</div>

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