0

I need to print the difference (in days) between the oldest day of file 1 ($2) and date of file 2 ($2) for each unique id ($1). so that I can have the Id ($1) and days difference as an output file.

File 1

102 2008-06-12 11:08:12 23
105 2005-05-16 20:15:05 15
102 2009-01-30 13:16:45 38
105 2006-06-14 15:30:25 45
102 2009-04-13 24:25:30 45
105 2006-07-23 13:23:30 12
108 2004-05-21 12:25:15 15
108 2005-04-29 06:15:13 23

File 2

102 20060305 13
105 20040530 12
108 20021225 21

Desired output

102 827 
105 351
108 511
  • What have you tried this far, and where in your progress are you stuck? Best regards. – maulinglawns Jan 15 '17 at 11:01
1

Whether you have GNU awk with mktime function

awk '
    FNR == NR{
        d = mktime(gensub("[-:]", " ", "g", $2 FS $3))
        if(D[$1] > d || ! D[$1])
            D[$1] = d
        next
    }
    {
        d = substr($2, 1, 4) FS substr($2, 5, 2) FS substr($2, 7) " 0 0 0"
        printf "%i %i\n", $1, (D[$1] - mktime(d)) / 86400
    }
    ' file1 file2

will produce

102 830
105 351
108 513
0

maybe this will help

#!/bin/bash
#changing to stamp
stamp1=$(date -d '2008-12-12 11:08:12' +"%s")
stamp2=$(date -d '20081210' +"%s")
#st2date1=$(date --date="@$stamp1" "+%Y-%m-%d %H:%M:%S")
#st2date2=$(date --date="@$stamp2" "+%Y-%m-%d %H:%M:%S")
#echo "$st2date1 -- $st2date2"
day='86400'
if [[ "$stamp1" -gt "$stamp2" ]]; then
  dif1=$(($stamp1-$stamp2))
  dif=$(( $dif1 / $day ))
  echo "Diffrent = $dif days"
else
  dif1=$(($stamp2-$stamp1))
  dif=$(( $dif1 / $day ))
  echo "Diffrent = $dif days"
fi

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.