0

I have 2 files with dates, which look like so:

# cat file_1
20190105
20171124
# cat file_2
20190112
20171201

How do I compare dates (line by line) and get a date range?

For example, something like this:

# cat final_file
20190105
...
20190112
20171124
...
20171201

2 Answers2

1

Something like?

#!/bin/bash
while read -r line_a && read -r line_b <&3; do
    seq ${line_a} ${line_b}
    echo "=========="
done < file_1 3<file_2

Output will be:

20190105
20190106
...
20190112
==========
20171124
20171125
20171126
20171127
...
20171200
20171201
==========
Nortol
  • 143
  • The problem with creating simple sequences is that you will also the values from 20171132 to 20171200, which are not valid dates. – U880D May 16 '18 at 08:17
0

From the given input and output files it is assumed the data is structed as follow:

cat -n FROM_DATE
     1  20190105
     2  20171124
cat -n TO_DATE
     1  20190112
     2  20171201

whereby the date has the format of YYYYmmdd (and was i.e. created via date +%Y%m%d).

To get a better idea about how it looks aggregated, lets print the two files side-by-side.

pr -m -J -t FROM_DATE TO_DATE
20190105        20190112
20171124        20171201

So it seems the questions is more about "How to expand the range of values which are splitted over two files and have the result aggregated?".

Following the hints from @steeldriver s comment and @Nortole s answer, a possible solution could look like:

#!/bin/bash
while read -r START_DATE && read -r END_DATE <&3; do

echo $START_DATE

    for (( DATE="${START_DATE}"; DATE != END_DATE; )); do
        DATE="$(date --date="${DATE} + 1 days" +'%Y%m%d')"
        echo $DATE # which will be equals to END_DATE at the end
    done

done < FROM_DATE 3<TO_DATE

Running the script will give the expected output:

20190105
20190106
20190107
20190108
20190109
20190110
20190111
20190112
20171124
20171125
20171126
20171127
20171128
20171129
20171130
20171201
U880D
  • 1,146