0

I have two files with dates:

File1

12/22/2017

File2

12/21/2017    
12/20/2017    
12/23/2017    

File1 will only have one record. File2 will have multiple records. I need to check if any of the dates in file2 is greater than the date in file1. Date Format in both files will be MM/DD/YYYY.

2
  • I need to check - how it should be expressed? how should look the output? Jan 9, 2018 at 11:17
  • If the condition is satisfied, then I need to echo "Date in file2 is greater than file1"
    – Harish
    Jan 9, 2018 at 12:27

3 Answers 3

2

Here is small script which sorts both files, greps for later dates than file1, and then counts (wc -l) if there are more than 1 uniq line (should be only 1 which comes from file1):

if [[ "$(sort -t/ -k3,3n -k1,1n -k2,2n file1 file2 | grep -A 1 -f file1 | uniq | wc -l)" -gt 1 ]]
    then
       echo "Date in file2 is greater than file1"
    else
       echo "Date in file2 is not greater than file1"
fi
1

GNU awk solution:

awk -F'/' '{ d=$3$1$2 }
           NR==FNR{ t=d; nextfile }
           d > t{ 
               print "file2 has date(s) greater than in file1";
               exit 
           }' file1 file2

The output:

file2 has date(s) greater than in file1
2
  • That's not my vote. Jan 9, 2018 at 13:28
  • Might be worth mentioning that this does require leading zeroes on both the day and month number. They could be added with sprintf() if necessary.
    – ilkkachu
    Jan 9, 2018 at 17:51
-1

Get the time in seconds from epoch (see man stat) and print the difference.

T1=$(stat --printf='%Y\n' file1.txt)
T2=$(stat --printf='%Y\n' file2.txt)

echo $(($T1 - $T2))
1
  • I think they meant they have files that contain lists of dates. Not that they wanted to handle the timestamps of the files themselves
    – ilkkachu
    Jan 9, 2018 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .