I have a file with rows like this:
2012/04/08 15:00,207
2012/04/08 16:00,180
I want to format the dates such that the file looks like:
08/04/2012 15:00,207
Awk would seem to be the right tool, but I can't get it to capture groups correctly. I've tried something like the following (Use sed or awk to fix date format):
awk '
BEGIN { FS = OFS = "," }
{ split($1, date, /\// )
$1 = date[3] "-" date[2] "-" date[1] " " date[4]
print $0
}
' $FILE
But it leaves with me data like this:
08 15:00-04-2012,207
Awk's split is obviously interpreting the time as part of the year field of the date string, and I can't seem to find a way to split it off separately. I've tried a number of different things like colons or escaped spaces in the split delimiter field but all give syntax error. Have looked at awk split's documentation and was not enlightened by it.
$ awk '{split($1,date,"/"); print date[3] "/" date[2] "/" date[1], $2}' file
08/04/2012 15:00,207
08/04/2012 16:00,180
FYI there's alternatives with GNU awk or sed for capture groups:
$ awk '{print gensub(/(....)\/(..)\/(..)/,"\\3/\\2/\\1",1)}' file
08/04/2012 15:00,207
08/04/2012 16:00,180
$ awk 'match($0,/(....)\/(..)\/(..)(.*)/,a){ print a[3]"/"a[2]"/"a[1] a[4] }' file
08/04/2012 15:00,207
08/04/2012 16:00,180
$ sed -r 's#(....)/(..)/(..)#\3/\2/\1#' file
08/04/2012 15:00,207
08/04/2012 16:00,180
Here is a solution that uses the regex fieldsplitting by setting FS to a regex that splits your timestamp apart and then recombines it:
awk '
BEGIN { FS= "[/ ]"; }
{print $3 "/" $2 "/" $1 " " $4 ;}
' file