bash 中的日期计算

发布于 2024-12-05 21:48:49 字数 399 浏览 0 评论 0原文

我有一个充满这种格式日期的文件(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清眉祭 2024-12-12 21:48:49

EDATE 未定义,也许您输入错误,它应该是 END-DATE

EDATE is not defined, maybe you made a typo and it should be END-DATE?

微凉 2024-12-12 21:48:49

您需要引用变量 EDATE 与 $EDATE(您真的是指 END-DATE 吗?)

START_DATE=`expr $EDATE - time`

(请注意,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 ?)

START_DATE=`expr $EDATE - time`

(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)

野の 2024-12-12 21:48:49

如果您只想要给定时间戳之前 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 page date(1) (that is, enter man 1 date).

秋风の叶未落 2024-12-12 21:48:49

我编写了一堆工具(dateutils)来解决此类问题,特别是dgrep 可能会有所帮助:

即兴发挥,我会选择

EDATE=$(tail -n1 MY_FILE)
THRES=$(dadd "${EDATE}" -5d)
dgrep ">=${THRES}" < MY_FILE

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

EDATE=$(tail -n1 MY_FILE)
THRES=$(dadd "${EDATE}" -5d)
dgrep ">=${THRES}" < MY_FILE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文