sách gpt4 ai đã đi

shell - So sánh ngày tháng trong các tập tin văn bản | shell

In lại 作者:行者123 更新时间:2023-12-04 08:03:13 25 4
mua khóa gpt4 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

1 Câu trả lời

尝试以下 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()+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
Kế tiếp
}
{ 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
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/

25 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com