I need a shells script which could parse incoming date values and print this in a standard format.
Incoming date patterns are:
"yyyyMMdd HHmmss"
"yyyyMMdd_HHmmss"
"MMddyyyy:HHmmss"
"MMddyyyyHHmmssmillisecond"
20170426 102300
20170426_102300
04262017:102300
0426201710230066
Output date pattern :
yyyyMMdd_HHmmSS 20170426_102300
Any idea how to achieve this result in bash. I tried couple of regex for getting result , but that didn't help. Any help is appreciated.
Is perl acceptable?
Havn't tested it though...
You could also use it like this:
For your array this may be acceptable:
If you don't have perl on your production-environment, you probably want to settle for a
sed
solution. I suggest the one from Walter A:Pipe the input to sed:
For fun, here is a solution using
awk
:Pretty much the same as the
perl
andsed
examples. Testing and regex replacing.First make all dates in the format yyyyMMdd
Next remove the optional character between day and hour
Change yyyyMMddHHmmss?? into desired format
EDIT: I first tried to show msec, but those were not needed: