# Grok: how to handle not found patterns

**URL:** <https://discuss.elastic.co/t/grok-how-to-handle-not-found-patterns/168323>\
**Category:** Logstash\
**Created:** [February 14, 2019, 2:49am UTC](https://discuss.elastic.co/t/grok-how-to-handle-not-found-patterns/168323 "2019-02-14T02:49:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shimpi](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@shimpi](https://discuss.elastic.co/u/shimpi)\
**Post date:** [February 14, 2019, 2:49am UTC](https://discuss.elastic.co/t/grok-how-to-handle-not-found-patterns/168323/1 "2019-02-14T02:49:58Z")

</div>

Hello Members,

I'm receiving this string as input event:  
` 2019-02-13 19:20:07,659 INFO 00ACDD5C000BAFFDC4D1353F49568680 [main] TEST (some_text)`

and I'm using pretty simple pattern:

> grok {  
> # grab the date, log level, gateway name and the component GUID.  
> match =\> { "message" =\> "%{TIMESTAMP\_ISO8601:date} \s\*.\* %{WORD:GUID} [.\*] %{WORD:GW}" }  
> # construct a file name.  
> add\_field =\> { "filename" =\> "%{[GW]}\_%{[GUID]}" }  
> }

In a nutshell, I'm capturing:

1. Timestamp
2. Ignoring the log level
3. GUID
4. Ignoring anything inside a square brace
5. A word as GW

The issue is, in the above log message, I may or may not get the word TEST. In short I might receive an event similar to:

> ```
> 2019-02-13 19:20:07,659 INFO 00ACDD5C000BAFFDC4D1353F49568680 [main] (some_text)
> 
> ```

If this happens, grok fails to parse anything.

As you could see, the TEST is missing from the log. In this case, I would like to either:

1. populate GW as an empty string, ""  
or
2. have (somehow) this:

> add\_field =\> { "filename" =\> "%{[GUID]}" }

Is this possible?

---

<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:** [February 14, 2019, 2:49pm UTC](https://discuss.elastic.co/t/grok-how-to-handle-not-found-patterns/168323/2 "2019-02-14T14:49:24Z")

</div>

You can make the GW field optional using

```
match => { "message" => "%{TIMESTAMP_ISO8601:date} \s*.* %{WORD:GUID} \[.*\] (%{WORD:GW})?" }

```

Then you can check to see if the field exists and take appropriate action

```
if [GW] { ...
```

---

<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:** [March 14, 2019, 2:49pm UTC](https://discuss.elastic.co/t/grok-how-to-handle-not-found-patterns/168323/3 "2019-03-14T14:49:34Z")

</div>

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