如何使用 unix shell 脚本将任何日期转换为 YYYYMMDDHHMMSS? [英] How to convert any date to YYYYMMDDHHMMSS using unix shell script?
问题描述
如何使用 unix shell 脚本将任何日期转换为 YYYYMMDDHHMMSS
?
它不是特定日期,它可能是任何日期.如何转换?
示例日期格式为:20110101050603
How to convert any date to YYYYMMDDHHMMSS
using unix shell script?
It is not specific date it may any date. how to convert it?
example date format is: 20110101050603
谢谢脓包
推荐答案
DATE = "15 Jun 2015 10:10:10"; date -d"$DATE" +%Y%m%d%H%M%S
输出:-
20150615101010
更多关于日期
请注意,这仅适用于 GNU 日期.
我读过:
More on Dates
Note this only works on GNU date.
I have read that:
Solaris 版本的 date,无法支持 -d 可以通过替换 sunfreeware.com 版本的 date 来解决.
Solaris version of date, which is unable to support -d can be resolve with replacing sunfreeware.com version of date.
更新:-
使用 ls
命令查找所有文件,使用 grep
命令查找所有文件.
Update:-
Use ls
command to find all files and use grep
command for all files.
ls | grep "[0-9]{14}"
grep
命令也接受正则表达式.
但它不会准确验证您的文件.例如,如果文件名是 20151835101010
,那么它会验证这个文件.但实际上月份和日期都是错误的.
所以我的建议是写一个shell脚本来识别有效的文件名,并通过一一处理找出其中最旧的.
grep
command also accepts regular expression.
But it won't validate your file accurate. For example, if a file name is 20151835101010
, then it will validate this file. But actually month and date both are wrong.
So my suggestion is to write a shell script to identify valid file name and find oldest of them by processing one by one.
这篇关于如何使用 unix shell 脚本将任何日期转换为 YYYYMMDDHHMMSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!