使用bash将日期列表从文件转换为时间戳 [英] Converting list of dates from a file to timestamp with bash
本文介绍了使用bash将日期列表从文件转换为时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用bash,我想将日期列表转换为"08-Sep-2020 07:08:49
"格式.时间戳记.
我发现了一种方式 ,在循环中逐行显示,但如果可能的话,我想在不使用循环的情况下转换整个文件.我正在考虑像"cat /tmp/file | <some_command>
"
I'm using bash and I would like to convert list of dates in format "08-Sep-2020 07:08:49
" to timestamp.
I found the way to do it one by one, line by line in a loop but I would like to convert whole file without using loops if possible. I was thinking about one line code like "cat /tmp/file | <some_command>
"
示例文件内容:
08-Sep-2020 03:07:04
08-Sep-2020 02:20:46
08-Sep-2020 23:25:52
我正在使用
date -d "09-Sep-2020 06:52:44" +%s
我想使用以下命令获取另一个文件或同一文件:
I would like to get another file or the same file with:
1599530824
1599528046
1599603952
推荐答案
使用GNU awk for mktime():
With GNU awk for mktime():
$ awk -F'[ :-]' '{print mktime($3" "((index("JanFebMarAprMayJunJulAugSepOctNovDec",$2)+2)/3)" "$1" "$4" "$5" "$6)}' file
1599552424
1599549646
1599625552
这篇关于使用bash将日期列表从文件转换为时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文