# Extracting particular folder from the path and adding that to a field

**URL:** <https://discuss.elastic.co/t/extracting-particular-folder-from-the-path-and-adding-that-to-a-field/142277>\
**Category:** Logstash\
**Created:** [July 31, 2018, 5:47am UTC](https://discuss.elastic.co/t/extracting-particular-folder-from-the-path-and-adding-that-to-a-field/142277 "2018-07-31T05:47:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![whoiscnu](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@whoiscnu](https://discuss.elastic.co/u/whoiscnu)\
**Post date:** [July 31, 2018, 5:47am UTC](https://discuss.elastic.co/t/extracting-particular-folder-from-the-path-and-adding-that-to-a-field/142277/1 "2018-07-31T05:47:38Z")

</div>

Hi Guys,

I need help to fetch particular folder from the path and assign the same to a different field.

path ==\> /var/mqm/qmgrs/FOLDER\_NEEDED/errors/\*.log.  
I need to extract FOLDER\_NEEDED and assign it to a field for every message.

Thanks.

---

<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:** [July 31, 2018, 11:35am UTC](https://discuss.elastic.co/t/extracting-particular-folder-from-the-path-and-adding-that-to-a-field/142277/2 "2018-07-31T11:35:19Z")

</div>

Are you saying that the events in logstash always have a field called path? And is the data you need to extract always the fourth directory name?

---

<div class="post-metadata">

**Author:** ![whoiscnu](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@whoiscnu](https://discuss.elastic.co/u/whoiscnu)\
**Post date:** [August 1, 2018, 4:08am UTC](https://discuss.elastic.co/t/extracting-particular-folder-from-the-path-and-adding-that-to-a-field/142277/3 "2018-08-01T04:08:11Z")

</div>

> [@Badger](#):
>
> s in logstash always have a field called path? And is the data you need to extract always the fourth directory nam

That is right. All messages have a field path.. I need to extract the folder from path and assign to a new field in the message

---

<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:** [August 1, 2018, 1:11pm UTC](https://discuss.elastic.co/t/extracting-particular-folder-from-the-path-and-adding-that-to-a-field/142277/4 "2018-08-01T13:11:18Z")

</div>

OK, so a UNIX path contain directory names separated by /. A directory name cannot contain /. So you could either go after the 4th directory name, or the last but one directory name. Either of these should work

```
    grok { match => ["message", "^/[^/]+/[^/]+/[^/]+/(?<dir1>[^/]+)" ] }
    grok { match => ["message", "/(?<dir2>[^/]+)/[^/]+/[^/]+$" ] }
```

---

<div class="post-metadata">

**Author:** ![whoiscnu](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@whoiscnu](https://discuss.elastic.co/u/whoiscnu)\
**Post date:** [August 2, 2018, 4:01pm UTC](https://discuss.elastic.co/t/extracting-particular-folder-from-the-path-and-adding-that-to-a-field/142277/5 "2018-08-02T16:01:11Z")

</div>

> [@Badger](#):
>
> "^/[^/]+/[^/]+/[^/]+/(?\<dir1\>[^/]+)" ]

Thanks  
match =\> { "path" =\> "/%{GREEDYDATA}/%{GREEDYDATA}/%{GREEDYDATA}/%{GREEDYDATA:folder}/%{GREEDYDATA}" } worked for me.

---

<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:** [August 2, 2018, 4:21pm UTC](https://discuss.elastic.co/t/extracting-particular-folder-from-the-path-and-adding-that-to-a-field/142277/6 "2018-08-02T16:21:19Z")

</div>

Generally, GREEDYDATA is going to be more expensive than [^/]+. It will work, it just burns a little more CPU.

---

<div class="post-metadata">

**Author:** ![whoiscnu](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@whoiscnu](https://discuss.elastic.co/u/whoiscnu)\
**Post date:** [August 3, 2018, 11:49am UTC](https://discuss.elastic.co/t/extracting-particular-folder-from-the-path-and-adding-that-to-a-field/142277/7 "2018-08-03T11:49:07Z")

</div>

> [@Badger](#):
>
> ns a little more CPU.

Thank You. Yeah.

---

<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:** [August 31, 2018, 11:49am UTC](https://discuss.elastic.co/t/extracting-particular-folder-from-the-path-and-adding-that-to-a-field/142277/8 "2018-08-31T11:49:09Z")

</div>

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