# Reversing The Order Of Characters to a date valid format

**URL:** https://discuss.elastic.co/t/reversing-the-order-of-characters-to-a-date-valid-format/233443
**Category:** Logstash
**Created:** [May 20, 2020, 3:11am UTC](https://discuss.elastic.co/t/reversing-the-order-of-characters-to-a-date-valid-format/233443 "2020-05-20T03:11:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Claudio\_Ract\_Costa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/claudio_ract_costa/32/26095_2.png) [@Claudio\_Ract\_Costa](https://discuss.elastic.co/u/Claudio_Ract_Costa)
#### Post date: [May 20, 2020, 3:11am UTC](https://discuss.elastic.co/t/reversing-the-order-of-characters-to-a-date-valid-format/233443/1 "2020-05-20T03:11:47Z")

</div>

Hi guys,

Does anyone know how can I reverse the order of the number bellow to transform it in a valid date format ?

I am trying to figure out how do it using mutate gsub or ruby but it is very complex.

from:  
024042315405313000

to:  
200424134550130300

And then to a valid date format:  
**20** 20-04-24T13:45:50.13 **0** Z

I need that because, unfortunately, my field comes in this format ☹

> "time\_triggertime\_occ":"024042315405313000"

---

<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: [May 20, 2020, 4:06pm UTC](https://discuss.elastic.co/t/reversing-the-order-of-characters-to-a-date-valid-format/233443/2 "2020-05-20T16:06:19Z")

</div>

Using mutate+gsub you could reverse a pair of characters using something like

```
mutate { gsub => { "someField", "(.)(.)(.*)", "\2\1\3" ] }
```

---

<div class="post-metadata">

### Author: ![Claudio\_Ract\_Costa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/claudio_ract_costa/32/26095_2.png) [@Claudio\_Ract\_Costa](https://discuss.elastic.co/u/Claudio_Ract_Costa)
#### Post date: [May 20, 2020, 7:46pm UTC](https://discuss.elastic.co/t/reversing-the-order-of-characters-to-a-date-valid-format/233443/3 "2020-05-20T19:46:33Z")

</div>

Hello Badger,

I was studying this piece of code.  
Each "(.)" is a block with one char and you use "\1", "\2", ... to indicate each block.

Just one problem ... when I have to indicate a block greater than 9, for example, "\10", gsub get "\1" block and after, insert "0"

For example, the code:

> gsub =\> ["time\_triggertime\_occ", "(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)", "\2\1\4\3\6\5\8\7' **\10'** \9"]

have the following output:

> "time\_triggertime\_occ" =\> "20042407'00'8",

the original value is:

> ```
> "time_triggertime_occ" => "024042708443313000",
> 
> ```

do you know how can i fix it ? (sorry for my english)

---

<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: [May 20, 2020, 9:01pm UTC](https://discuss.elastic.co/t/reversing-the-order-of-characters-to-a-date-valid-format/233443/4 "2020-05-20T21:01:34Z")

</div>

After a lot of Googling and searching of forums I have learned the following: Back-references to numbered captures in Ruby regexps are limited to 9. If you want more you will have to use named captures.

For example

```
input { generator { count => 1 lines => ['abcdefghijkl'] } }
filter {
    mutate {
        gsub => [
            "message",
            "(?<d1>.)(?<d2>.)(?<d3>.)(?<d4>.)(?<d5>.)(?<d6>.)(?<d7>.)(?<d8>.)(?<d9>.)(?<d10>.)(?<d11>.)(?<d12>.)",
            "\k<d1> \k<d2> \k<d3> \k<d4> \k<d5> \k<d6> \k<d7> \k<d8> \k<d9> \k<d10> \k<d11> \k<d12>"
        ]
    }

```

will result in

```
   "message" => "a b c d e f g h i j k l",
```

---

<div class="post-metadata">

### Author: ![Claudio\_Ract\_Costa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/claudio_ract_costa/32/26095_2.png) [@Claudio\_Ract\_Costa](https://discuss.elastic.co/u/Claudio_Ract_Costa)
#### Post date: [May 20, 2020, 9:48pm UTC](https://discuss.elastic.co/t/reversing-the-order-of-characters-to-a-date-valid-format/233443/5 "2020-05-20T21:48:53Z")

</div>

Espetacular, Badger !

It worked !! Thanks my friend !!

Bellow is the final code:

```
   mutate {
        gsub => [
            "time_triggertime_occ",
            "(?<d1>.)(?<d2>.)(?<d3>.)(?<d4>.)(?<d5>.)(?<d6>.)(?<d7>.)(?<d8>.)(?<d9>.)(?<d10>.)(?<d11>.)(?<d12>.)(?<d13>.)(?<d14>.)(?<d15>.)(?<d16>.)(?<d17>.)(?<d18>.)",
            "20\k<d2>\k<d1>-\k<d4>\k<d3>-\k<d6>\k<d5>T\k<d8>\k<d7>:\k<d10>\k<d9>:\k<d12>\k<d11>.\k<d14>\k<d13>0-\k<d16>\k<d15>\k<d18>\k<d17>"
        ]
    }

```

The output was:

> "time\_triggertime\_occ" =\> "2020-04-24T07:48:34.130-0300",

---

<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: [June 17, 2020, 10:03pm UTC](https://discuss.elastic.co/t/reversing-the-order-of-characters-to-a-date-valid-format/233443/6 "2020-06-17T22:03:21Z")

</div>

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