# Logstash condition to check if a field value starts with another field's value

**URL:** <https://discuss.elastic.co/t/logstash-condition-to-check-if-a-field-value-starts-with-another-fields-value/264168>\
**Category:** Logstash\
**Created:** [February 12, 2021, 6:28pm UTC](https://discuss.elastic.co/t/logstash-condition-to-check-if-a-field-value-starts-with-another-fields-value/264168 "2021-02-12T18:28:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattinVS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattinvs/32/83861_2.png) [@mattinVS](https://discuss.elastic.co/u/mattinVS)\
**Post date:** [February 12, 2021, 6:28pm UTC](https://discuss.elastic.co/t/logstash-condition-to-check-if-a-field-value-starts-with-another-fields-value/264168/1 "2021-02-12T18:28:10Z")

</div>

Hi,

I'd like to know how I can check in an if-condition in Logstash, is a field's value (in the example below the field is named "complete") starts with the value of another field (in the example below the field is named "starts\_with").

I tried

```auto
if [complete] =~ /^[starts_with].*/ {
    ...
}

```

This sounds like a simple task, but maybe it's just too late for me. Do you have a syntax hint?

Best regards, Mattin

---

<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 12, 2021, 8:14pm UTC](https://discuss.elastic.co/t/logstash-condition-to-check-if-a-field-value-starts-with-another-fields-value/264168/2 "2021-02-12T20:14:18Z")

</div>

I do not think there is a way to do that with a conditional. You may need to resort to ruby

```
ruby { code => 'event.set("[@metadata][testThis]", event.get("complete").start_with? (event.get("starts_with")))' }
if [@metadata][testThis] {
    ...
```

---

<div class="post-metadata">

**Author:** ![mattinVS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattinvs/32/83861_2.png) [@mattinVS](https://discuss.elastic.co/u/mattinVS)\
**Post date:** [February 12, 2021, 10:49pm UTC](https://discuss.elastic.co/t/logstash-condition-to-check-if-a-field-value-starts-with-another-fields-value/264168/3 "2021-02-12T22:49:28Z")

</div>

Thanks, that's a pity that one needs ruby for such a simple task.  
But at least it explains why I couldn't get it working without ruby 😃

Best regards, Mattin

---

<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 12, 2021, 10:50pm UTC](https://discuss.elastic.co/t/logstash-condition-to-check-if-a-field-value-starts-with-another-fields-value/264168/4 "2021-03-12T22:50:06Z")

</div>

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