-3
Yes, before someone jumps up and attack me with a pitchfork, this is a duplicate question, but the others don't work for me, so I'm asking it myself now.
I have a CSV file that have dates somewhere in each entry. Added difficulty to the conversion is that sometimes the dates will have single digits for days. Example entries:
abc,0,2,-2,3-16-1994
xyz,1,2,3,10-09-1994
I want something, preferably sed, to convert those data entries to look like this:
abc,0,2,-2,1994-03-16
xyz,1,2,3,1994-09-10
I've tried:
sed 's|(..)-(..)-(....)|\3-\2-\1|'
But that gives an error and it doesn't really cover the single digit day issues.
I also tried:
awk -F - '{print $3$2$1}'
This actually has somewhat of a desired effect, but then again not really. The awk command converts it, but only the month and year, and instead of putting the date back where it was, it put's the month and year of the beginning of the line, leaving the day portion wherever it originally was.
Any help would be awesome!
Thanks in advance.
Edit
It was rightly pointed out in the comments that I made a mistake with my examples. The dates should be:
abc,0,2,-2,16-03-1994
xyz,1,2,3,2-05-1994
With the desired result being:
abc,0,2,-2,1994-03-16
xyz,1,2,3,1994-05-02
Sorry guys.
Can you explain what about the other solutions didn't work? Are they just not the final format you want, or are they not appropriate for the input you're dealing with, or something else? – music2myear – 2017-04-26T21:40:51.800
@music2myear I explained what went wrong with the awk solution. The sed command actually threw an error, complaining about something being missing in "RE", an error I've never seen before. – user1840352 – 2017-04-26T21:44:33.647
Have you looked further into that error? – music2myear – 2017-04-26T21:53:51.490
As John C points out in his answer, you say your data are ‘‘dd-mm-yyyy’’, but your example data include ‘‘3-16-1994’’, which means the 3rd day of the 16th month — so your question is flawed.
– Scott – 2017-04-27T01:46:37.497@Scott I fixed my question with an edit at the bottom. Sorry for the misleading mistake. – user1840352 – 2017-04-27T13:46:22.147
Please don't fix errors by adding an addendum that says "the above is wrong; it should be X "; fix them *by fixing them* (in place). – Scott – 2017-04-27T19:06:02.030