# Extracting year in short format from the log file name

**URL:** <https://discuss.elastic.co/t/extracting-year-in-short-format-from-the-log-file-name/327172>\
**Category:** Logstash\
**Created:** [March 7, 2023, 11:49am UTC](https://discuss.elastic.co/t/extracting-year-in-short-format-from-the-log-file-name/327172 "2023-03-07T11:49:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lupsya](https://avatars.discourse-cdn.com/v4/letter/l/3ec8ea/32.png) [@lupsya](https://discuss.elastic.co/u/lupsya)\
**Post date:** [March 7, 2023, 11:49am UTC](https://discuss.elastic.co/t/extracting-year-in-short-format-from-the-log-file-name/327172/1 "2023-03-07T11:49:22Z")

</div>

Hello,

I am extracting Year, Month, and Day from the following testing log name and converting it to timestamp later.

`log20230225.log`

I am using the following grok filter:  
`log%{YEAR:year}%{MONTHNUM:month}%{MONTHDAY:day}\.log`

The fields extracted are:

```auto
year: 2023
month: 02
day: 25

```

The problem is that real log names are in a different format: `log230225.log` where the year is in the short format using just the first 2 digits for the year, so the %{YEAR:year} does not work here, as it is expecting 4 digits input.

Is there a way to extract the year as 2 digits only by grok? Something like %{YEAR:year\_short}? or maybe just extract those 2 first digits to a separate field and I can use them later for the timestamp conversion using yy?

---

<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:** [March 7, 2023, 5:38pm UTC](https://discuss.elastic.co/t/extracting-year-in-short-format-from-the-log-file-name/327172/2 "2023-03-07T17:38:01Z")

</div>

You could try

```
log(?<year>\d{2})%{MONTHNUM:month}%{MONTHDAY:day}\.log

```

---

<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:** [April 4, 2023, 5:38pm UTC](https://discuss.elastic.co/t/extracting-year-in-short-format-from-the-log-file-name/327172/3 "2023-04-04T17:38:36Z")

</div>

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