datetime - Convert time from dd/mm/yyyy hh:mm:ss to unix timestamp in bash script -
i have browsed through similar threads , helped me come closest want didn't answer question.
i have date in format dd/mm/yyyy hh:mm:ss
($mydate = 26/12/2013 09:42:42
) want convert in unix timestamp via command:
date -d $mydate +%s
but here accepted format one: yyyy-mm-dd hh:mm:ss
so did transformation:
echo $mydate| awk -f' ' '{printf $1}'| awk -f/ '{printf "%s-%s-%s\n",$3,$2,$1}'
and have ouput:
2013-12-26
which good, try append hour part before doing conversion:
echo $mydate| awk -f' ' '{printf $1; $hour=$2}'| awk -f/ '{printf "%s-%s-%s %s\n",$3,$2,$1,$hour}'
but have this:
2013-12-26 26/12/2013
it seem not keep variable $hour
.
i new in awk, how ?
in awk can use regex field separator. in case instead of using awk twice may want following:
echo $mydate| awk -f' |/' '{printf "%s-%s-%s %s",$3,$2,$1,$4}'
with use both space , / separators. first 3 parts date field, 4th 1 time lies after space.
Comments
Post a Comment