bash 中的日期计算
我有一个充满这种格式日期的文件(2002-09-26 02:20:30), 我想从文件末尾提取最后 5 天的时间,这是我写的
END-DATE=tail -1 my file (which is 2002-09-26 02:20:30)
time=$(expr 60 * 60 * 24 * 5) ( counting 5days which is 432000)
up to know every thing is ok ! the problem is with next line,
START-DATE=`expr END-DATE - time`
似乎是错误的: expr: non-numeric argument
我应该如何将此时间转换为纪元时间?
I have a file full of dates with this format (2002-09-26 02:20:30),
I want to extract the last 5 days from the end of the file , here is what I wrote
END-DATE=tail -1 my file (which is 2002-09-26 02:20:30)
time=$(expr 60 * 60 * 24 * 5) ( counting 5days which is 432000)
up to know every thing is ok ! the problem is with next line,
START-DATE=`expr END-DATE - time`
seems it's wrong : expr: non-numeric argument
how should I convert this time to epoch time ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
EDATE
未定义,也许您输入错误,它应该是END-DATE
?EDATE
is not defined, maybe you made a typo and it should beEND-DATE
?您需要引用变量 EDATE 与 $EDATE(您真的是指 END-DATE 吗?)
(请注意,shell 变量名称中不能有 - ,因此 START-DATE 和 END-DATE 无效。将它们命名为 START_DATE 和而是END_DATE)
You need to refer to the variable, EDATE vs $EDATE (did you really mean END-DATE ?)
(Note that you cannot have a - in shell variable names, so START-DATE and END-DATE are invalid. Name them START_DATE and END_DATE rather)
如果您只想要给定时间戳之前 5 天的日期并且安装了 GNU Coreutils,则可以使用
date -d "$(tail -n 1 some/file.ext) 5 days ago"
;如果您希望采用特定格式,请尝试查看手册页date(1)
(即输入man 1 date
)。If you just want the date 5 days before a given timestamp and have GNU Coreutils installed, you could use
date -d "$(tail -n 1 some/file.ext) 5 days ago"
; if you want that in a particular format, try looking at the man pagedate(1)
(that is, enterman 1 date
).我编写了一堆工具(dateutils)来解决此类问题,特别是
dgrep
可能会有所帮助:即兴发挥,我会选择
I've written a bunch of tools (dateutils) to tackle exactly these kinds of problems, in particular
dgrep
might help:Off the cuff, I'd go for