From this question copy and replace column using unix
I have tried to build a solution which works only from this file:
20070101 10.2317 79.1638 6.0 26.7 20.9 0.8 14.0 98.6
20070102 10.2317 79.1638 5.6 26.5 20.8 1.9 13.6 98.0
20070103 10.2317 79.1638 7.5 27.7 20.8 0.1 15.8 96.4
20070104 10.2317 79.1638 8.1 26.0 19.6 0.0 15.5 94.1
And get the output:
01/01/2007 10.2317 79.1638 6.0 26.7 20.9 0.8 14.0 98.6
02/01/2007 10.2317 79.1638 5.6 26.5 20.8 1.9 13.6 98.0
03/01/2007 10.2317 79.1638 7.5 27.7 20.8 0.1 15.8 96.4
04/01/2007 10.2317 79.1638 8.1 26.0 19.6 0.0 15.5 94.1
Without other files involved.
So I want to substitute the first column by the converted date.
I got the date from the original file with:
$ awk '{print $1}' filedate.txt
20070101
20070102
20070103
20070104
Then I did the date conversion with:
for i in $(awk '{print $1}' filedate.txt); do date -d "$i" +%d/%m/%Y; done
01/01/2007
02/01/2007
03/01/2007
04/01/2007
But I haven't been able to modify the first column of the file by the date values already converted. I tried with awk substitution (awk '{$1=$dt}1'
):
for i in $(awk '{print $1}' filedate.txt); do dt=$(date -d "$i" +%d/%m/%Y) && awk '{$1=$dt}1' filedate.txt; done
But as there is a loop involved, the output is not the desired one.
How can I achieve this with awk
? Is it possible to do the same with sed
?
EDIT
In the comments of the other question I saw the following way with sed
sed 's,^\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\),\3/\2/\1,'
But I'm now curious about how to do it involving the date
command.