I need some help on date conversion in-place on a CSV. Basically, I was capable to convert the column and save in a new file, but I was unable to save it on the original CSV.
I have a CSV with date format DD/MM/YYYY HH:MM
and I want to convert to YYYY-MM-DD HH:MM
, it's the first column of my CSV.
My CSV file has something like that:
29/01/2018 14:07,payable,37159871,,30521316
29/01/2018 14:07,payable,37159872,,30521316
29/01/2018 14:07,payable,37159870,,30521316
29/01/2018 14:07,payable,37159869,,30521316
29/01/2018 14:07,payable,37159868,,30521316
And I want to have something like that:
2018-01-29 14:07,payable,37159871,,30521316
2018-01-29 14:07,payable,37159872,,30521316
2018-01-29 14:07,payable,37159870,,30521316
2018-01-29 14:07,payable,37159869,,30521316
2018-01-29 14:07,payable,37159868,,30521316
What I was capable to do:
gawk -F, '{split($1, a, "/| "); print a[3]"-"a[2]"-"a[1]" "a[4]}' /path/to/file.csv > test_file
So now I want to know how can I save this back on my CSV file.
awk
, just rename it later. – Weijun Zhou Jan 29 '18 at 20:01gawk
you may be able to use the in-place extension – steeldriver Jan 29 '18 at 20:04sponge
in moreutils – thrig Jan 29 '18 at 20:08mv test_file /path/to/file.csv
, to overwrite the old file with the new file afterwards. – nath Jan 29 '18 at 20:08test_file
only contain the first column of the CSV file... – Max Forasteiro Jan 29 '18 at 20:14$2, $3, $4
to your awk command, setOFS=,
if necessary. If the file isn't too big (it doesn't seem to be),sponge
may save you some trouble in writing back to the original file. – Weijun Zhou Jan 29 '18 at 20:17