0

I have the following table is in csv format

07-04-2017 , YES
08-04-2017 ,YES
09-04-2017 , YES
10-04-2017 , YES
11-04-2017 , YES
07-04-2017 , YES

I want the first column to be compared with the current date of the system and if the date mentioned in the first column is in the past, then I want to change the 2nd column to "NO". The resultant table is:-

07-04-2017 ,NO
08-04-2017 ,NO
09-04-2017 ,YES
10-04-2017 ,YES
11-04-2017 ,YES
07-04-2017 ,NO

I tried awk statement but it is not comparing dates.Kindly share the command if you know how to go about this problem.

2
  • where's the comma in comma-separated-value format? and please add whatever awk command you tried Commented Apr 9, 2017 at 15:42
  • date=$(date '+%m/%d/%Y'); awk -F "," '{ if($2<date) {print "YES"} else {print "NO"}}' But this is returning only "NO" Commented Apr 10, 2017 at 5:32

3 Answers 3

2

Try this -

$awk -v date=$(date '+%d-%m-%Y') '{print ($1 < date?$1 FS "NO": $1 FS "YES")}' f
07-04-2017 NO
08-04-2017 NO
09-04-2017 YES
10-04-2017 YES
11-04-2017 YES
07-04-2017 NO

As per the comment - Todays's result for this command -

$awk -v date=$(date '+%d-%m-%Y') '{print ($1 < date?$1 FS "NO": $1 FS "YES")}' f
07-04-2017 NO
08-04-2017 NO
09-04-2017 NO
10-04-2017 YES
11-04-2017 YES
07-04-2017 NO
Sign up to request clarification or add additional context in comments.

5 Comments

It is returning only "YES"
@BShiva - It is working fine, check my added result.
if you are only getting "YES" means $1 is always less than 07-04-2017 possibilities are high that date is wrong in your machine, paste the output of date '+%d-%m-%Y' command.
I had tried applying it in a table which had the date in the second column and had changed $1 to $2. But after separating the column the proper result came. Thank You.
Good to hear that it solved your Query :) stackoverflow.com/help/someone-answers
1

If I understand your question correctly, then there is no need to rely on any other utility. You can make use of POSIX shell parameter expansion and substring removal to parse the year, month, and day and put them together in a reformatted date string that can be used with date -d "string" to get the number of seconds since epoch (1970-01-01 00:00:00 UTC) and compare that against the current date/time to determine if the date read is before the current. You can then output "NO" if it is.

Example:

#!/bin/sh

while IFS=$' \t\n,' read -r fdate ans
do
    yr="${fdate##*-}"    ## parse year
    tmp="${fdate%-$yr}"  ## parse tmp="day-month"
    day="${tmp%-*}"      ## parse day
    mo="${tmp#*-}"       ## parse month
    rfdate="${yr}-${mo}-${day}"   ## create "year-mo-day" string

    ## compare date string with current day/time (seconds since epoch)
    if [ "$(date -d "$rfdate" +%s)" -lt "$(date +%s)" ]
    then
        echo "$fdate, NO"
    else
        echo "$fdate, $ans"
    fi
done < dat/dates 

Example Input

$ cat dat/dates
07-04-2017 ,YES
08-04-2017 ,YES
09-04-2017 ,YES
10-04-2017 ,YES
11-04-2017 ,YES
07-04-2017 ,YES

Example Use/Output

$ sh datebeforetst.sh <dat/dates
07-04-2017, NO
08-04-2017, NO
09-04-2017, NO
10-04-2017, NO
11-04-2017, YES
07-04-2017, NO

You can adjust how the current day is compared if you need to consider 'today' not in the past, for example (now - 24 hours):

if [ "$(date -d "$rfdate" +%s)" -lt "$(($(date +%s)-86400))" ]

results in output of:

$ sh datebeforetst.sh <dat/dates
07-04-2017, NO
08-04-2017, NO
09-04-2017, NO
10-04-2017, YES
11-04-2017, YES
07-04-2017, NO

Comments

0

Here's a bash version

#!/bin/bash

DATE=`date +%d-%m-%Y`
while read L
do
    D=`echo $L | cut -f1 -d\ `
    if [ $D \< $DATE ]
    then
        echo "$D NO"
    else
        echo "$D YES"
    fi
done <./dates.txt

All in one line:

DATE=`date +%d-%m-%Y`; while read L; do D=`echo $L | cut -f1 -d\ `;[ $D \< $DATE ] && echo "$D NO" || echo "$D YES"; done <./dates.txt

2 Comments

The above code is returning only "YES" if I write it in one line. In script it gives this error:: length.ksh[5]: [: <: unknown operator
It's probably because you are using ksh. Use bash instead. I'll write a ksh version later when I have time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.