简体   繁体   English

如何在shell脚本中将字符串转换为时间戳

[英]how to convert a string to timestamp in shell script

i have a string "Fri Nov 3 17:50:23 2017" like this 我有一个像这样的字符串“ Fri Nov 3 17:50:23 2017”

how to convert this String to Timestamp(2017-11-03T17:50:23) using shell script in Linux 如何在Linux中使用Shell脚本将此字符串转换为Timestamp(2017-11-03T17:50:23)

Please help me. 请帮我。 sagar 萨加尔

date --date='Fri Nov 3 17:50:23 2017' +"%Y-%m-%d %H:%M:%S"

ps如果您想要的输出中'T'不是拼写错误,只需用'T'改变空格

#!/bin/bash
#Declare
zero=0
Time_S=T
Jan=1
Feb=2
Mar=3
Nov=11
     string="Fri Nov 3 17:50:23 2017"
     s1=$(echo $string|cut -d " " -f5)
     s2=$(echo $string|cut -d " " -f2)
     s3=$(echo $string|cut -d " " -f3)
     s4=$Time_S$(echo $string|cut -d " " -f4)
     if [ $s3 -lt 10 ];then
        echo "valuse is less than 10"
         s3=$zero$s3
    else
        s3=$(($s3))
    fi
     echo "$s1-$(($s2))-$s3$s4"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM