gpt4 book ai didi

shell - 过滤文本文件中的日期

转载 作者:行者123 更新时间:2023-12-04 16:18:06 25 4
gpt4 key购买 nike

我有一个文本文件,其中包含以下格式的数据。我们如何只打印那些日期距当前日期少于 10 天的行?
样本.txt

system system_data8 Thu Jul 29 22:36:38 2021
system system_data9 Wed Jan 24 14:43:52 2018
system system_data3 Tue Jan 23 20:25:17 2018
system system_data2 Fri Mar 09 20:37:05 2018
system system_data5 Fri Mar 09 22:02:31 2018
预期输出
system system_data8 Thu Jul 29 22:36:38 2021
我正在尝试类似下面的方法,但它不起作用。
awk -F ' ' '{printf("%s,%s,",$1,$2);"date +%F -d "$3" "$4" "$5" "$6" "$7;}' sample.txt

最佳答案

将 GNU awk 用于时间函数:

$ cat tst.awk
BEGIN {
tgtDays = 10
tgtSecs = tgtDays * 24 * 60 * 60

endTime = strftime("%Y %m %d 12 00 00")
endSecs = mktime(endTime,1)
}
{
mthNr = (index("JanFebMarAprMayJunJulAugSepOctNovDec",$4)+2)/3
begTime = sprintf("%04d %02d %02d 12 00 00", $7, mthNr, $5)
begSecs = mktime(begTime,1)
}
(endSecs - begSecs) < tgtSecs
$ awk -f tst.awk sample.txt
system system_data8 Thu Jul 29 22:36:38 2021
请注意,在上面我们用中午替换了输入数据和当前时间中的时间,因为在通过将时间戳转换为自纪元以来的秒数然后除以秒数来确定两个日期之间的天数时day 您必须每天使用相同的时间,否则您的“天数”计算可能/将被每天的时间抛弃。
例如,查看以下试图确定相隔 10 天的 2 个日期是否相隔小于 10 天的以下内容:
$ cat diffDatesDemo.awk
BEGIN {
tgtDays = 10
tgtSecs = tgtDays * 24 * 60 * 60

begTime = "2021/08/01 09:00:00"
endTime = "2021/08/11 08:00:00"

begDate = gensub(/([ :][0-9]{2}){3}$/,"",1,begTime)
endDate = gensub(/([ :][0-9]{2}){3}$/,"",1,endTime)

print "Is", begTime, "less than", tgtDays, "days before", endTime "?"

####
print "\nWrong: Compare 2 timestamps including date plus time of day:"
begSecs = mktime(gensub("[/:]"," ","g",begTime),1)
endSecs = mktime(gensub("[/:]"," ","g",endTime),1)

print begDate, "->", endDate, "is", ((endSecs - begSecs) < tgtSecs ? "<" : ">="), tgtDays, "days"
####

####
print "\nRight: Compare 2 dates at the same time each day:"
begSecs = mktime(gensub("[/:]"," ","g",begDate)" 12 00 00",1)
endSecs = mktime(gensub("[/:]"," ","g",endDate)" 12 00 00",1)

print begDate, "->", endDate, "is", ((endSecs - begSecs) < tgtSecs ? "<" : ">="), tgtDays, "days"
####
}
$ awk -f diffDatesDemo.awk
Is 2021/08/01 09:00:00 less than 10 days before 2021/08/11 08:00:00?

Wrong: Compare 2 timestamps including date plus time of day:
2021/08/01 -> 2021/08/11 is < 10 days

Right: Compare 2 dates at the same time each day:
2021/08/01 -> 2021/08/11 is >= 10 days
我还为 mktime() 使用了 UTC 标志以确保任何本地 DST 更改不会影响天数计算。

关于shell - 过滤文本文件中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68608564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com