# Need advice in logstash to dynamicly output csv file name based on weekly timestamp

**URL:** <https://discuss.elastic.co/t/need-advice-in-logstash-to-dynamicly-output-csv-file-name-based-on-weekly-timestamp/188197>\
**Category:** Logstash\
**Created:** [June 30, 2019, 4:32pm UTC](https://discuss.elastic.co/t/need-advice-in-logstash-to-dynamicly-output-csv-file-name-based-on-weekly-timestamp/188197 "2019-06-30T16:32:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aso/32/49098_2.png) [@aso](https://discuss.elastic.co/u/aso)\
**Post date:** [June 30, 2019, 4:32pm UTC](https://discuss.elastic.co/t/need-advice-in-logstash-to-dynamicly-output-csv-file-name-based-on-weekly-timestamp/188197/1 "2019-06-30T16:32:09Z")

</div>

So i'm doing a csv output automation where i want my exported csv file to be named like  
**data-2019.07.01.csv**  
but naming/creating the new file only occur every monday (weekly) so the file will contain data from July 1st-7th,  
and the next monday logstash would create:  
**data-2019.07.08.csv** (will contain data from july 8th-14th).

Maybe odd, but i'm thinking like adding new field which has value day of year like:  
**day: "Mon"**

so when it's monday the output will be like:  
`path => "/tmp/data-%{+yyyy.MM.dd}"`

but on other day like:  
**day: "Tue"**

the output will be like:  
`path => "/tmp/data-%{+yyyy.MM.**(dd-1)**}"`

The whole output will be like:

> ```
> output {
> if [day] == "Mon"
> csv {
> path => "/tmp/data-%{+yyyy.MM.dd}"
> }
> } else if [day] == "Tue" {
> csv {
> path => "/tmp/data-%{+yyyy.MM.(dd-1)}"
> }
> } else if [day] == "Wed" {
> csv {
> path => "/tmp/data-%{+yyyy.MM.(dd-2)}"
> }
> } ... and so on
> }
> 
> ```

\*Event better if the file could be named data-2019.07.01-2019.07.07.csv.

is that close to posibility? Hope someone can help. thanks in advance.

---

<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:** [June 30, 2019, 5:08pm UTC](https://discuss.elastic.co/t/need-advice-in-logstash-to-dynamicly-output-csv-file-name-based-on-weekly-timestamp/188197/2 "2019-06-30T17:08:20Z")

</div>

Apologies if looking at my ruby code makes your eyeballs bleed, but this will do it

```
    ruby {
        code => '
            def date_of_prev(day)
                date = Date.parse(day)
                # <= means if today is the day we return today
                delta = date <= Date.today ? 0 : -7
                date + delta
            end

            dThis = date_of_prev "Wednesday"
            dNext = (dThis + 7).strftime "%Y.%m.%d"
            dThis = dThis.strftime "%Y.%m.%d"
            event.set("[@metadata][indexname]", "#{dThis}-#{dNext}")
        '
    }
```

---

<div class="post-metadata">

**Author:** ![aso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aso/32/49098_2.png) [@aso](https://discuss.elastic.co/u/aso)\
**Post date:** [June 30, 2019, 5:48pm UTC](https://discuss.elastic.co/t/need-advice-in-logstash-to-dynamicly-output-csv-file-name-based-on-weekly-timestamp/188197/4 "2019-06-30T17:48:17Z")

</div>

sorry i edited my question on title and the body cause some typo, i will definitely try in the office tomorrow

---

<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 28, 2019, 5:48pm UTC](https://discuss.elastic.co/t/need-advice-in-logstash-to-dynamicly-output-csv-file-name-based-on-weekly-timestamp/188197/5 "2019-07-28T17:48:18Z")

</div>

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