1

I tried the various solutions offered in the question of which mine appears to be a duplicate. Some of them did not actually convert the time, one of them did convert the time but the year was wrong and it did not replace the existing value in the file, it added a new column to the output. The answer I accepted below met my needs perfectly.

Sorry if this is a duplicate, I searched extensively and didn't find questions matching my situation. I use Linux frequently but my "programming" skills are limited to simple shell scripts.

I have a csv file with a column of date/time stamps written in the epoch (i.e. Unix) format. Please see below example. I would like to convert and replace the Unix time with the ISO 8601 format.

Example of existing output:

foo, 1439856000000000000, foo, foo
foo, 1439856360000000000, foo, foo
foo, 1439856720000000000, foo, foo
foo, 1439857080000000000, foo, foo

Example of Desired output:

foo, YYYY-MM-DD HH:MM:SS.nnnnnnnnn, foo, foo
foo, YYYY-MM-DD HH:MM:SS.nnnnnnnnn, foo, foo
foo, YYYY-MM-DD HH:MM:SS.nnnnnnnnn, foo, foo
foo, YYYY-MM-DD HH:MM:SS.nnnnnnnnn, foo, foo

1 Answers1

1

I'd use perl:

perl -MPOSIX -pe 's{\b\d{10}(?=\d{9}\b)}{
  strftime("%Y-%m-%d %T.", localtime $&)}ge'

(here giving the time in local time, use gmtime instead of localtime for GMT times)