This isn't something you can do with a bash command, you need an external program since bash can't do floating point math and can't handle dates. I suspect you just meant "on the command line" though, and you don't actually require a pure bash solution. With that assumption, here's a gawk
(GNU awk) solution:
$ gawk '{$1=strftime("%d.%m.%Y %H:%m:%S",sprintf("%.3f", $1 /1000))}1;' file
02.04.2018 19:04:24 entity1,sometext
02.04.2018 19:04:41 entity2,sometext
02.04.2018 19:04:47 entity1,sometext
The strftime
function converts the timestamp to a date. Since yours is in milliseconds, we use sprintf
to convert that to seconds. The final 1;
just tells gawk
to print the line after we have assigned the new value to $1
(the first field).
Remember that the output will depend on your timezone. For example, the above was run on a machine set to the GMT timezone. This is why my numbers are different to the ones in @thanasisp's answer which was presumably run on a different timezone. You can control this and change the output by setting the TZ
variable when running the command. For example, compare this:
$ gawk '{$1=strftime("%d.%m.%Y %H:%m:%d.%S",sprintf("%.3f", $1 /1000))}1;' file
02.04.2018 19:04:02.24 entity1,sometext
02.04.2018 19:04:02.41 entity2,sometext
02.04.2018 19:04:02.47 entity1,sometext
to this:
$ TZ=America/Los_Angeles gawk '{$1=strftime("%d.%m.%Y %H:%m:%d.%S",sprintf("%.3f", $1 /1000))}1;' file
02.04.2018 11:04:02.24 entity1,sometext
02.04.2018 11:04:02.41 entity2,sometext
02.04.2018 11:04:02.47 entity1,sometext
Or this:
$ TZ=Asia/Tokyo gawk '{$1=strftime("%d.%m.%Y %H:%m:%d.%S",sprintf("%.3f", $1 /1000))}1;' file
03.04.2018 03:04:03.24 entity1,sometext
03.04.2018 03:04:03.41 entity2,sometext
03.04.2018 03:04:03.47 entity1,sometext
I tried it with awk and sed as well. same with perl.
Can be also YYYY.MM.DD. But in my country its just different. we use DD.MM.YYYY. But you are right, when I need to process the data later, its easiert this way
– fastboot Dec 05 '20 at 15:11