AWK将任务列表中的字符串日期值与今天的日期进行比较 [英] AWK comparing string date value from task list to today's date
问题描述
我有一个todo.txt任务列表,我希望对其进行过滤,以显示计划在将来或今天进行的任何任务,即-不显示过去的日期,而仅显示已计划日期的任务. /p>
有时文件行和命令会更改为包含阈值(请考虑贪睡/推迟任务,直到...)"日期,格式为:t:date +%Y-%m-%d,表示请勿开始这个任务直到这个日期".
数据文件:
50 (A) Testing due date due:2018-09-22 t:2018-09-25
04 (B) Buy Socks, Underwear t:2018-09-22
05 (B) Buy Vaporizer t:2018-09-23 due:2018-09-22
16 (C) Watch Thor Ragnarock
12 (B) Pay Electric Bill due:2018-09-20 t:2018-09-25
x 2018-09-21 pri:B Buy Prebiotics +health @web due:2018-09-21
到目前为止,我已经提出了这个建议:
cat t | awk -F: -v date="$(date +%Y-%m-%d)" '/due:|t:/ $2 >= date || $3 >= date { print $0}'|
nl
问题是,日期比较正在到期:"字段上进行,因为它通常出现在"t:"字段之前.此外,还会输出比今天早的条目.
输出:
1 50 (A) Testing due date due:2018-09-22 t:2018-09-25
2 05 (B) Buy Vaporizer t:2018-09-23 due:2018-09-22
3 12 (B) Pay Electric Bill due:2018-09-20 t:2018-09-25
问题:
-
如果存在"t:",如何正确地将日期与:"分隔符后的"t:"值进行比较-如果不存在"t:",则应与到期时间"值进行正确比较?
-
大于(>")的日期似乎有效,但等于((>>")
$ cat tst.awk
{
orig = $0
sched = ""
for (i=NF; i>0; i--) {
if ( sub(/^t:/,"",$i) ) {
sched = $i
break
}
else if ( sub(/^due:/,"",$i) ) {
sched = $i
}
}
$0 = orig
}
sched >= date
$ awk -v date="$(date +%Y-%m-%d)" -f tst.awk file
50 (A) Testing due date due:2018-09-22 t:2018-09-25
05 (B) Buy Vaporizer t:2018-09-23 due:2018-09-22
12 (B) Pay Electric Bill due:2018-09-20 t:2018-09-25
I have a todo.txt task list that I'd like to filter on to show any tasks scheduled for dates in the future or today, i.e. - show no past dates scheduled and only show tasks which have a date scheduled.
The file lines and orders change sometimes to include a 'threshold (think snooze/postpone task until...) date in format: t:date +%Y-%m-%d which says, 'don't start this task until this date'.
Data file:
50 (A) Testing due date due:2018-09-22 t:2018-09-25
04 (B) Buy Socks, Underwear t:2018-09-22
05 (B) Buy Vaporizer t:2018-09-23 due:2018-09-22
16 (C) Watch Thor Ragnarock
12 (B) Pay Electric Bill due:2018-09-20 t:2018-09-25
x 2018-09-21 pri:B Buy Prebiotics +health @web due:2018-09-21
So far I've come up with this:
cat t | awk -F: -v date="$(date +%Y-%m-%d)" '/due:|t:/ $2 >= date || $3 >= date { print $0}'|
nl
Problem is, The date comparison is working on the "due:" field as it usually comes before "t:" field. Also, entries older then today are output.
Output:
1 50 (A) Testing due date due:2018-09-22 t:2018-09-25
2 05 (B) Buy Vaporizer t:2018-09-23 due:2018-09-22
3 12 (B) Pay Electric Bill due:2018-09-20 t:2018-09-25
Questions:
How do I correctly make the date comparison against "t:" value after the ":" separator if "t:" is present - and on the "due:" value if "t:" is not present?
Date greater than (">") seems to work but equal to does not (">=")
$ cat tst.awk
{
orig = $0
sched = ""
for (i=NF; i>0; i--) {
if ( sub(/^t:/,"",$i) ) {
sched = $i
break
}
else if ( sub(/^due:/,"",$i) ) {
sched = $i
}
}
$0 = orig
}
sched >= date
$ awk -v date="$(date +%Y-%m-%d)" -f tst.awk file
50 (A) Testing due date due:2018-09-22 t:2018-09-25
05 (B) Buy Vaporizer t:2018-09-23 due:2018-09-22
12 (B) Pay Electric Bill due:2018-09-20 t:2018-09-25
这篇关于AWK将任务列表中的字符串日期值与今天的日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!