在 Shell 中转换日期
如何在 shellscript 中将一种日期格式转换为另一种格式?
示例:
旧格式是
MM-DD-YY HH:MM
,但我想将其转换为
YYYYMMDD.HHMM
How can I convert one date format to another format in a shellscript?
Example:
the old format is
MM-DD-YY HH:MM
but I want to convert it into
YYYYMMDD.HHMM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就像
“20${D:6:2}${D:0:2}${D:3:2}.${D:9:2}${D:12:2}00”< /code>,如果
$D
变量中是旧日期。Like
"20${D:6:2}${D:0:2}${D:3:2}.${D:9:2}${D:12:2}00"
, if the old date in the$D
variable.利用 shell 的分词和位置参数:
Take advantage of the shell's word splitting and the positional parameters:
请注意,如果您有一个大文件要处理,那么上述过程是一个昂贵的过程。对于基于文件的数据,我建议类似
output
....|....|20111221.235900|20111222.000100| ...|
我希望这有帮助。
Note that if you have a large file to process, then the above is an expensive(ish) process. For filebased data I would recommend something like
output
....|....|20111221.235900|20111222.000100| ...|
I hope this helps.
已经有一个很好的答案了,但是你说你想要在评论中找到替代方案,所以这是我的[相比之下相当糟糕]方法:
所以,第一行只读取一个日期,然后我们得到两位数的年份,然后我们根据它是否小于 50 将其附加到
'20'
或'19'
(因此这将为您提供从 1950 年到 2049 年的年份 - 请随意切换线)。然后我们附加一个连字符以及月份和日期。然后我们附加一个空格和时间,最后我们附加':00'
作为秒(再次随意设置您自己的默认值)。最后,我们使用 GNU 日期来读取它(因为它现在已经标准化)并以不同的格式打印它(您可以编辑)。它比切割字符串更长、更难看,但在最后一行保留格式可能是值得的。您也可以使用您在第一个答案中刚刚学到的速记法来大大缩短它。
祝你好运。
There is a good answer down already, but you said you wanted an alternative in the comments, so here is my [rather awful in comparison] method:
So, the first line just reads a date in, then we get the two-digit year, then we append it to
'20'
or'19'
based on if it's less than 50 (so this would give you years from 1950 to 2049 - feel free to shift the line). Then we append a hyphen and the month and date. Then we append a space and the time, and lastly we append':00'
as the seconds (again feel free to make your own default). Lastly we use GNU date to read it in (since it's been standardized now) and print it in a different format (which you can edit).It's a lot longer and uglier than cutting up the string, but having the format in the last line may be worth it. Also you could shorten it significantly with the shorthand you just learned in the first answer.
Good luck.