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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -