I have a file containing many columns of text, including a timestamp along the lines of Fri Jan 02 18:23
and I need to convert that date into MM/DD/YYYY HH:MM
format.
I have been trying to use the standard `date' tool with awk getline to do the conversion, but I can't quite figure out how to pass the fields into the 'date' command in the format it expects (quoted with " or 's,) as getline needs the command string enclosed in quotes too.
Something like "date -d '$1 $2 $3 $4' +'%D %H:%M'" | getline var
Now that I think about it, I guess what I'm really asking is how to embed awk variables into a string.
I had a similar issue converting a date from RRDTool databases using rrdfetch but prefer one liners that I've been using since Apollo computer days.
Data looked like this:
One liner:
Result:
On the face of it this doesn't look very efficient to me but this kind of methodology has always proven to be fairly low overhead under most circumstances even for very large files on very low power computer (like 25Mhz NeXT Machines). Yes Mhz.
Sed deletes the colon, awk is used to print the other various commands of interest including just echoing the awk variables and sh or bash executes the resulting string.
For methodology or large files or streams I just head the first few lines and gradually build up the one liner. Throw away code.
you can try this. Assuming just the date you specified is in the file
output
and if you are not using GNU tools, like if you are in Solaris for example, use nawk
If you're using gawk, you don't need the external
date
which can be expensive to call repeatedly:Thanks to ghostdog74 for the months array from this answer.