[英]Bash get epoch time from custom string date
我有一個文本文件,其內容中有自定義日期,如下所示:
2018/Nov/28 file1
2019/Jan/17 file2
and so on
如何將2019/Jan/17
部分轉換為紀元格式?
使用 BSD date
(macOS 附帶):
$ date -jf '%Y/%B/%d' +%s "2018/Nov/28"
1543442700
-j
選項阻止您實際設置系統時鍾; -f
指定輸入格式, +%s
提供 output 格式。
Perl 可以做到:
$ cat file
2018/Nov/28 file1
2019/Jan/17 file2
$ perl -MTime::Piece -ape '$F[0] = Time::Piece->strptime($F[0], "%Y/%b/%d")->epoch; $_ = "@F\n"' file
1543363200 file1
1547683200 file2
我建議安裝dateutils並使用strptime
:
$ strptime -i "%Y/%B/%d" -f "%s%n" '2019/Jan/17'
1547683200
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.