# Grok rewrite field value if matches string

**URL:** https://discuss.elastic.co/t/grok-rewrite-field-value-if-matches-string/66958
**Category:** Logstash
**Created:** [November 23, 2016, 8:58am UTC](https://discuss.elastic.co/t/grok-rewrite-field-value-if-matches-string/66958 "2016-11-23T08:58:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Arthur\_Francis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arthur_francis/32/13065_2.png) [@Arthur\_Francis](https://discuss.elastic.co/u/Arthur_Francis)
#### Post date: [November 23, 2016, 8:58am UTC](https://discuss.elastic.co/t/grok-rewrite-field-value-if-matches-string/66958/1 "2016-11-23T08:58:00Z")

</div>

Hello,

I am trying to extract a value from a log message that can take various forms, I have the 2 different regex patters which work individually.

if the first regex produces a certain field value I want to rewrite the field with a different regex pattern.

```
grok {
                patterns_dir => ["./patterns"]
                match => ["message", "%{GREEDYDATA}\n%{JAVA_EXCEPTION_SHORT:exception}"]
        }
if [exception] =~ "Caused by" {
                grok {
                patterns_dir => ["./patterns"]
                match => ["message", "%{GREEDYDATA}\n%{JAVA_EXCEPTION_LONG:exception}"]
}

```

the above line did not execute as expected, could somebody please suggest a possible solution?

Much appreciated in advance

---

<div class="post-metadata">

### Author: ![Arthur\_Francis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arthur_francis/32/13065_2.png) [@Arthur\_Francis](https://discuss.elastic.co/u/Arthur_Francis)
#### Post date: [November 23, 2016, 4:13pm UTC](https://discuss.elastic.co/t/grok-rewrite-field-value-if-matches-string/66958/2 "2016-11-23T16:13:45Z")

</div>

I have tried something different, and now running through the logs hopefully will work.

```
grok {
                patterns_dir => ["./patterns"]
                match => ["message", "%{GREEDYDATA}\n%{JAVA_EXCEPTION_SHORT:exception}"]
        }

        if "Caused by" in [exception] {
                mutate {
                    remove_field => "exception"
                }
                grok {
                    patterns_dir => ["./patterns"]
                    match => ["message", "%{GREEDYDATA}\n%{JAVA_EXCEPTION_LONG:exception}"]
                }
        }
```

---

<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: [December 21, 2016, 4:14pm UTC](https://discuss.elastic.co/t/grok-rewrite-field-value-if-matches-string/66958/3 "2016-12-21T16:14:00Z")

</div>

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