gpt4 book ai didi

shell - 比较文本文件中的日期| shell

转载 作者:行者123 更新时间:2023-12-04 08:03:13 24 4
gpt4 key购买 nike

我有一个包含第 7 列日期的文件,我的要求是将它与今天的日期进行比较,如果小于它,则删除该完整行。
此外,如果第 7 列中提到的任何日期超过 15 天,则将其修改为最多 15 天
下面的例子-

now="$(date +'%d-%m-%Y')"
now=24-02-2021
文件.txt
abc    xyz        pqr NFS     5       abc.vol
abc xyz pqr NFS 50 xyz.bal 23-02-2021
abc xyz pqr NFS 5 abcd.xyz
abc xyz pqr NFS 15 pqr.vol 25-02-2023
abc xyz pqr NFS 5 xyz.bal 24-03-2021
abc xyz pqr NFS 3 pqrst.vol 19-01-2019
输出应该是-
abc    xyz        pqr NFS     5       abc.vol
abc xyz pqr NFS 5 abcd.xyz
abc xyz pqr NFS 15 pqr.vol 11-03-2021
abc xyz pqr NFS 5 xyz.bal 24-03-2021
我尝试通过 awk 但它完全不起作用
awk -v d=$now '$7 == "" && $7 < d' file.txt

最佳答案

尝试以下 GNU awk 解决方案:

awk 'NF == 6 { print;next } { OFS="\t";split($7,map,"-");depoch=mktime(map[3]" "map[2]" "map[1]" 00 00 00");if (depoch<systime()) { next };if (+depoch>(systime()+1296000)) { $7=strftime("%d-%m-%Y",(systime()+1296000)) } }1' file.txt
解释:
awk 'NF == 6 { print;                                                    # If there is no date, print the line and skip to the next
next
}
{ OFS="\t"; # Set the output field delimiter to tab
split($7,map,"-"); # Split the date into the array map (getting day, month and year)
depoch=mktime(map[3]" "map[2]" "map[1]" 00 00 00"); # Create epoch format of the date
if (depoch<systime()) {
next # If epoch format of date is less than today, skip to the next record
};
if (depoch>(systime()+1296000)) {
$7=strftime("%d-%m-%Y",(systime()+1296000)) # If the date is greater than today +15 days, set the date to the date + 15 days (1296000 seconds) and format accordingly using strftime
}
}1' file.txt
我假设发布的所需输出不正确。 24-03-2021 比今天晚了 15 天,所以应该改成 11-03-2021

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

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