There is a variable which contains 20190105221953.ogg
,
Want it turns to "2019-01-05 19:59:53"
Tried to apply regex with needed quantity of numbers
grep -Eo '[[:digit:]]{4}'
But It's not working.
There is a variable which contains 20190105221953.ogg
,
Want it turns to "2019-01-05 19:59:53"
Tried to apply regex with needed quantity of numbers
grep -Eo '[[:digit:]]{4}'
But It's not working.
You can try converting your string to datetime with date
command:
date -d "${var:0:8} ${var:8:2}:${var:10:2}:${var:12:2}" +"%Y-%m-%d %H:%M:%S"
This may help for flexible output of the date by changing "%Y-%m-%d %H:%M:%S"
Thanks, PesaThe!
var=`date +"%Y%m%d%H%M%S"`.ogg
echo ${var:0:4}-${var:4:2}-${var:6:2} ${var:8:2}:${var:10:2}:${var:12:2}
Though it seems awkward to use sed
... :
echo $var
#=> 20190105221953.ogg
echo $var|sed -E 's/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2}).*/\1-\2-\3 \4:\5:\6/'
#=> 2019-01-05 22:19:53