# Remove part of message string BEFORE and AFTER config

**URL:** <https://discuss.elastic.co/t/remove-part-of-message-string-before-and-after-config/220470>\
**Category:** Logstash\
**Created:** [February 22, 2020, 5:01am UTC](https://discuss.elastic.co/t/remove-part-of-message-string-before-and-after-config/220470 "2020-02-22T05:01:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![isarib](https://avatars.discourse-cdn.com/v4/letter/i/b9e5f3/32.png) [@isarib](https://discuss.elastic.co/u/isarib)\
**Post date:** [February 22, 2020, 5:01am UTC](https://discuss.elastic.co/t/remove-part-of-message-string-before-and-after-config/220470/1 "2020-02-22T05:01:09Z")

</div>

Hi,

Message: /merlion/dpa2/cn133ta/j4\_sryuo/j4\_02\_sv\_ip

I am trying to find a way to remove the strings BEFORE and AFTER a defined string in a message.

In this message, the constant value is dpa2, and I want to discard anything BEFORE dpa2(inclusive), and the "cn133ta/". Only want to retain "j4\_sryuo/j4\_02\_sv\_ip".

The position of cn133ta will be of dynamic value, so it is not a constant string. Hope that makes sense.

At the moment, I have tried the gsub below:

mutate { gsub =\> ["dir\_path", "/[^./]+/tpa2/", "/" ] }

But I'm having trouble finding a way to discard the AFTER string, in this case "cn133ta/".

Thanks

---

<div class="post-metadata">

**Author:** ![rugenl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rugenl/32/12887_2.png) [@rugenl](https://discuss.elastic.co/u/rugenl)\
**Post date:** [February 22, 2020, 3:00pm UTC](https://discuss.elastic.co/t/remove-part-of-message-string-before-and-after-config/220470/2 "2020-02-22T15:00:32Z")

</div>

Is it always / separated? Use dissect or grok to break it into fields, then remove\_field the ones you don't want.

---

<div class="post-metadata">

**Author:** ![isarib](https://avatars.discourse-cdn.com/v4/letter/i/b9e5f3/32.png) [@isarib](https://discuss.elastic.co/u/isarib)\
**Post date:** [February 24, 2020, 1:58am UTC](https://discuss.elastic.co/t/remove-part-of-message-string-before-and-after-config/220470/3 "2020-02-24T01:58:54Z")

</div>

Yes, its always separated with a "/", since it is really a form of directory structure.

I have found the solution, and that is the below.

```auto
mutate { gsub => ["dir_path", "^.*?/dpa2/[^/]+/", "" ] }

```

---

<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 23, 2020, 1:59am UTC](https://discuss.elastic.co/t/remove-part-of-message-string-before-and-after-config/220470/4 "2020-03-23T01:59:07Z")

</div>

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