[英]Date Comparison in file Shell Scripting
[I have a file which has second column date in format mm/dd/yyyy. [我有一个文件,其第二列日期格式为 mm/dd/yyyy。
ASVC038M8 09/10/2019
ASVC066M8 11/04/2020
ASVC075M8 11/07/2024
ASVC068M8 11/08/2020
ASVC069M8 11/13/2020
ASVC070M8 11/14/2020
ASVC047M8 12/08/2019
ASVC051M8 12/08/2019
ASVC037M8 12/21/2019
ASVC052M8 12/21/2019
ASVC043M8 12/28/2019
ASVC040M8 12/28/2019
I want to get information for those entries which have date more than 35 Days ago.. My current code is我想获取日期超过 35 天的条目的信息。我当前的代码是
Date=$(date +"%m/%d/%y" -d "+35 days")
cat test.txt | awk -v date="$Date" '$2 > date { print $1" "$2 }')
In this, I am getting the output as在此,我得到 output 作为
ASVC043M8 12/28/2019
ASVC040M8 12/28/2019
Not sure why I am not getting all the entries as there are few more entries which have a date more than 35 days ago.不知道为什么我没有收到所有条目,因为日期超过 35 天的条目很少。
Help, please.请帮忙。
thanks in advance.提前致谢。
Could you please try following(date command should be GNU date
)您能否尝试以下操作(日期命令应该是 GNU date
)
Date=$(date +"%Y-%m-%d" -d "+35 days")
awk -v date="$Date" '(substr($2,7)"-"substr($2,1,2)"-"substr($2,4,2)) > date' Input_file
OR as per Ed sir's suggestion in comments with split
try:或根据 Ed sir 在split
尝试的评论中的建议:
Date=$(date +"%Y-%m-%d" -d "+35 days")
awk -v date="$Date" '{split($2,array,"/")} (array[3]"-"array[1]"-"array[2]) > date' Input_file
You can use awk
mktime
to convert dates into sortable dates, or use bash string processing.您可以使用awk
mktime
将日期转换为可排序的日期,或者使用 bash 字符串处理。 Using awk
will be faster - important for large files.使用awk
会更快 - 对于大文件很重要。
awk -v from=$(date -d '35 days ago' +'%Y-%m-%d') '
{ dt = substr($2, 7) "-" substr($2, 1, 2) "-" substr($2, 4, 2) ;
if ( dt < from ) print
}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.