# Logstash - Trim value

**URL:** <https://discuss.elastic.co/t/logstash-trim-value/138379>\
**Category:** Logstash\
**Created:** [July 3, 2018, 12:27pm UTC](https://discuss.elastic.co/t/logstash-trim-value/138379 "2018-07-03T12:27:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkaramol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkaramol/32/22610_2.png) [@pkaramol](https://discuss.elastic.co/u/pkaramol)\
**Post date:** [July 3, 2018, 12:27pm UTC](https://discuss.elastic.co/t/logstash-trim-value/138379/1 "2018-07-03T12:27:59Z")

</div>

I have huge json files with logs, which are all timestamped in the form

```auto
result":{"@timestamp":"2016-11-30T21:59:59.699265Z"

```

The problem is that due to some reason, the timestamp format is in various forms, e.g. the following may also occur

```auto
"result":{"@timestamp":"2016-11-30T21:56:28.000Z"

```

I am able to parse correctly the second using this pattern:

```auto
yyyy-MM-dd'T'HH:mm:ss.000Z

```

but adjusting it to the first one will not work:

```auto
yyyy-MM-dd'T'HH:mm:ss.000000Z

```

What I want is to **trim** the field and only keep its first 19 characters, so as to throw away any `XXXXXZ` patterns;

How can anyone **trim** a field in logstash?

The following is not working:

```auto
  mutate {
        add_field => ["custom_time", "%{[result][@timestamp]}" ]
      }

.
.
.
    ruby {
      code => '
        event.set("custom_time", "custom_time"[0...19])
      '
    }

```

---

<div class="post-metadata">

**Author:** ![pkaramol](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkaramol/32/22610_2.png) [@pkaramol](https://discuss.elastic.co/u/pkaramol)\
**Post date:** [July 3, 2018, 1:43pm UTC](https://discuss.elastic.co/t/logstash-trim-value/138379/2 "2018-07-03T13:43:27Z")

</div>

OK that worked:

```
  mutate {
    add_field => ["custom_time", "%{[result][@timestamp]}" ]
  }

  ruby {
      code => '
        timevar = event.get("custom_time")
        event.set("custom_time", timevar[0...19])
       '
    }

    date {
        match => ["[custom_time]", "yyyy-MM-dd'T'HH:mm:ss" ]
        tag_on_failure => ["no_date_match"]
    }
```

---

<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 31, 2018, 1:46pm UTC](https://discuss.elastic.co/t/logstash-trim-value/138379/3 "2018-07-31T13:46:37Z")

</div>

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