转换日期格式在bash [英] Convert date formats in bash

查看:151
本文介绍了转换日期格式在bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个格式的日期:2011年6月27日,我想将其转换为20110627

是否有可能在bash做?


解决方案

  #since这是昨天
日期-dyesterday +%Y%m%d日#more precise,更reccomended
日期-d'27六月2011+%Y%m%d日#assuming这是类同昨天从`你date`问题
#http://stackoverflow.com/q/6497525/638649
日期-d'last-星期一'+%Y%m%d日#going对@赛斯的评论,你可以这样做
日期=2011年6月27日;日期-d$ DATE+%Y%m%d日#OR的方法,从标准输入读取它
阅读-p获取日期>>中日期; printf的为YYYYMMDD格式>>%S`日期
-d$ DATE+%Y%M%D`#which然后输出以下:
#获取日期>> 2011年6月27日
#AS YYYYMMDD格式>> 20110627#如果你真的想用awk
回声2011年6月27日| AWK'{打印日期-d \\$ 1FS $ 2FS $ 3\\+%Y%M%D}'|庆典#note |庆典只是重定向awks要执行输出到shell
#FS的字段分隔符,在这种情况下,你可以使用$ 0打印线
#But这是有用的,如果你有一个以上的日期上线

<一个href=\"http://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html#Date-input-formats\">More在日期

请注意在GNU日这仅适用

我已阅读:


  

的Solaris版本日期,这是无法
  支持 -d 可与解决
  更换的sunfreeware.com版本
  日期


I have a date in this format: "27 JUN 2011" and I want to convert it to 20110627

Is it possible to do in bash?

解决方案

#since this was yesterday
date -dyesterday +%Y%m%d

#more precise, and more reccomended
date -d'27 JUN 2011' +%Y%m%d

#assuming this is similiar to yesterdays `date` question from you 
#http://stackoverflow.com/q/6497525/638649
date -d'last-monday' +%Y%m%d

#going on @seth's comment you could do this
DATE = "27 jun 2011"; date -d"$DATE" +%Y%m%d

#or a method to read it from stdin
read -p "  Get date >> " DATE; printf "  AS YYYYMMDD format >> %s"  `date
-d"$DATE" +%Y%m%d`    

#which then outputs the following:
#Get date >> 27 june 2011   
#AS YYYYMMDD format >> 20110627

#if you really want to use awk
echo "27 june 2011" | awk '{print "date -d\""$1FS$2FS$3"\" +%Y%m%d"}' | bash

#note | bash just redirects awks output to the shell to be executed
#FS is field separator, in this case you can use $0 to print the line
#But this is useful if you have more than one date on a line

More on Dates

note this only works on GNU date

I have read that:

Solaris version of date, which is unable to support -d can be resolve with replacing sunfreeware.com version of date

这篇关于转换日期格式在bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆