AWK comparing string date value from task list to

2019-07-29 12:44发布

问题:

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:

  1. 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?

  2. Date greater than (“>”) seems to work but equal to does not (“>=“)

回答1:

$ 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