I have two files with dates:





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.

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

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 ]]
       echo "Date in file2 is greater than file1"
       echo "Date in file2 is not greater than file1"

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";
           }' file1 file2

The output:

file2 has date(s) greater than in file1
  • That's not my vote. – glenn jackman Jan 9 '18 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 '18 at 17:51

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))
  • 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 '18 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.