关于 UNIX 可变日期格式
我有一个日期值存储在 Unix KornShell (ksh) 脚本变量中:
VALUE="2010_09_23"
我想将其更改为“2010 年 9 月 23 日”并将其存储在 VALUE1 中。我该怎么做?
VALUE1="23-Sep-2010"
I have a date value stored in a Unix KornShell (ksh) script variable:
VALUE="2010_09_23"
I want to change it to "23-Sep-2010" and store it in VALUE1. How can i do it?
VALUE1="23-Sep-2010"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Ksh 通过内置的
printf
具有内置的日期格式化功能。文档并不清楚参数应该如何格式化;以下内容在我的 ksh 93s+ 中有效:Ksh has a built-in date formatting capability through the
printf
builtin. The documentation isn't clear about how the argument should be formatted; the following works in my ksh 93s+:使用可移植的 awk 和硬编码的月份名称列表。
substr
调用解决了一个棘手的问题,即在某些awk
实现中对数字“08”和“09”的解释:它们是十进制数字“8”和“ 9' 或无效的八进制数?Using portable
awk
and a hardcoded list of month names. Thesubstr
calls work around a tricky issue is the interpretation of the numbers '08' and '09' in someawk
implementations: Are they the decimal numbers '8' and '9' or invalid octal numbers?使用 Perl:
输出:
Using Perl:
Output:
此命令在 OSX 上执行您想要的操作:
如果您使用不同的 Unix 变体,您需要的
date
选项组合可能会有所不同。%
表示法已得到很好的标准化(请参阅man strftime
),但date
命令的功能却并非如此。GNU coreutils
date
(你在 Linux 上得到的东西,也可能隐藏在你的“SunOS”系统的某个地方(我怀疑你实际上是指“SunOS”,Sun Microsystems 还没有大约十五年来都没有这样称呼它的 Unix 变体,但即便如此,/usr/bin
中的东西仍然是令人难以置信的过时垃圾)——查看/usr/local
> 和/opt
的所有子目录 - 也可以执行此操作,但您必须预处理输入:这可能也可以工作,具体取决于您拥有的 shell:
This command does what you want on OSX:
If you are using a different Unix variant, the combination of options to
date
that you require may be different. The%
-notation is well standardized (seeman strftime
) but the capabilities of thedate
command are not.GNU coreutils
date
(what you get on Linux, and which may also be hiding somewhere on your "SunOS" system (I doubt you actually mean "SunOS", Sun Microsystems hasn't called its Unix variant that for about fifteen years, but even so, the stuff in/usr/bin
is still incredibly obsolete junk) -- look in/usr/local
and all subdirectories of/opt
-- can also do this but you have to preprocess the input:This may also work, depending on exactly which shell you have: