# Extract timestamp from filename

**URL:** https://discuss.elastic.co/t/extract-timestamp-from-filename/56135
**Category:** Logstash
**Created:** [July 21, 2016, 8:01pm UTC](https://discuss.elastic.co/t/extract-timestamp-from-filename/56135 "2016-07-21T20:01:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![subbu.nv](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@subbu.nv](https://discuss.elastic.co/u/subbu.nv)
#### Post date: [July 21, 2016, 8:01pm UTC](https://discuss.elastic.co/t/extract-timestamp-from-filename/56135/1 "2016-07-21T20:01:47Z")

</div>

Hi All,

currently i have filename with the below format  
[XXXXXXXX][YYYYYYYYYY][2016\_07\_21][19\_21\_12][160721T192103][ZZZZ]AB\_RTRT.0.log.  
is there a way, i can extract the datetimestamp and index it to a specific field in elastic search.

thanks  
Subbu

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [July 21, 2016, 8:13pm UTC](https://discuss.elastic.co/t/extract-timestamp-from-filename/56135/2 "2016-07-21T20:13:06Z")

</div>

I assume you're getting the filename in the `path` field. Use a grok filter to extract the timestamp from `path` into a field of its own.

---

<div class="post-metadata">

### Author: ![subbu.nv](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@subbu.nv](https://discuss.elastic.co/u/subbu.nv)
#### Post date: [July 22, 2016, 7:55am UTC](https://discuss.elastic.co/t/extract-timestamp-from-filename/56135/3 "2016-07-22T07:55:51Z")

</div>

thanks for the direction.

i used the below grok and i can fetch the year, month and date ([2016\_07\_21][19\_21\_12])  
grok {  
match =\> ["filename", "%{YEAR:year}_%{MONTHNUM:month}_%{MONTHDAY:day}"]  
add\_field =\> ["date", "%{month}/%{day}/%{year} "]  
}

but i am not sure how to fetch the hour, mins and secs.

can you please help?

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [July 22, 2016, 8:13am UTC](https://discuss.elastic.co/t/extract-timestamp-from-filename/56135/4 "2016-07-22T08:13:11Z")

</div>

Always format regular expressions as code. If you look closely at your post you'll note that it doesn't represent reality. All underscores have disappeared.

Unsurprisingly, capturing a time works the same way as capturing a date. I suggest the following expression for capturing the whole timestamp string (including the square brackets):

```
(?<timestamp>\[%{YEAR}_%{MONTHNUM}_%{MONTHDAY}\]\[%{HOUR}_%{MINUTE}_%{SECOND}\])

```

This string can then be fed to the date filter.

---

<div class="post-metadata">

### Author: ![subbu.nv](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@subbu.nv](https://discuss.elastic.co/u/subbu.nv)
#### Post date: [July 22, 2016, 9:36pm UTC](https://discuss.elastic.co/t/extract-timestamp-from-filename/56135/5 "2016-07-22T21:36:27Z")

</div>

thanks a lot it works.

i used this in grok,

match =\> ["filename", "(?[%{YEAR}_%{MONTHNUM}_%{MONTHDAY}][%{HOUR}_%{MINUTE}_%{SECOND}])"]

and applied this in a date filter

date{

```
match => ["temptimestamp", "[yyyy_MM_dd][HH_mm_ss]"]
    target => "filetimestamp"

```

}

it works fine. thanks a lot.

---

<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: [July 6, 2017, 4:46am UTC](https://discuss.elastic.co/t/extract-timestamp-from-filename/56135/6 "2017-07-06T04:46:44Z")

</div>


