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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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






share|improve this question


















  • 3




    show your efforts
    – RomanPerekhrest
    Nov 20 '17 at 17:54










  • Are you looking for a range based on year, or month? Could you sort the file and just print the first and last line? $ cat final_file | sort
    – SpruceTips
    Nov 20 '17 at 19:55







  • 1




    Possibly related: Store all dates in a given date range into a variable
    – steeldriver
    Nov 20 '17 at 19:58














up vote
0
down vote

favorite












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






share|improve this question


















  • 3




    show your efforts
    – RomanPerekhrest
    Nov 20 '17 at 17:54










  • Are you looking for a range based on year, or month? Could you sort the file and just print the first and last line? $ cat final_file | sort
    – SpruceTips
    Nov 20 '17 at 19:55







  • 1




    Possibly related: Store all dates in a given date range into a variable
    – steeldriver
    Nov 20 '17 at 19:58












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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






share|improve this question














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








share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '17 at 18:14









Pierre.Vriens

94241015




94241015










asked Nov 20 '17 at 17:46









Oleg Kalinin

82




82







  • 3




    show your efforts
    – RomanPerekhrest
    Nov 20 '17 at 17:54










  • Are you looking for a range based on year, or month? Could you sort the file and just print the first and last line? $ cat final_file | sort
    – SpruceTips
    Nov 20 '17 at 19:55







  • 1




    Possibly related: Store all dates in a given date range into a variable
    – steeldriver
    Nov 20 '17 at 19:58












  • 3




    show your efforts
    – RomanPerekhrest
    Nov 20 '17 at 17:54










  • Are you looking for a range based on year, or month? Could you sort the file and just print the first and last line? $ cat final_file | sort
    – SpruceTips
    Nov 20 '17 at 19:55







  • 1




    Possibly related: Store all dates in a given date range into a variable
    – steeldriver
    Nov 20 '17 at 19:58







3




3




show your efforts
– RomanPerekhrest
Nov 20 '17 at 17:54




show your efforts
– RomanPerekhrest
Nov 20 '17 at 17:54












Are you looking for a range based on year, or month? Could you sort the file and just print the first and last line? $ cat final_file | sort
– SpruceTips
Nov 20 '17 at 19:55





Are you looking for a range based on year, or month? Could you sort the file and just print the first and last line? $ cat final_file | sort
– SpruceTips
Nov 20 '17 at 19:55





1




1




Possibly related: Store all dates in a given date range into a variable
– steeldriver
Nov 20 '17 at 19:58




Possibly related: Store all dates in a given date range into a variable
– steeldriver
Nov 20 '17 at 19:58










2 Answers
2






active

oldest

votes

















up vote
1
down vote













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
==========





share|improve this answer






















  • 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 at 8:17


















up vote
0
down vote













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





share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405822%2fhow-to-compare-dates-line-by-line-and-get-a-date-range%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    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
    ==========





    share|improve this answer






















    • 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 at 8:17















    up vote
    1
    down vote













    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
    ==========





    share|improve this answer






















    • 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 at 8:17













    up vote
    1
    down vote










    up vote
    1
    down vote









    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
    ==========





    share|improve this answer














    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
    ==========






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '17 at 12:23

























    answered Nov 22 '17 at 12:16









    Nortole

    145




    145











    • 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 at 8:17

















    • 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 at 8:17
















    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 at 8:17





    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 at 8:17













    up vote
    0
    down vote













    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





    share|improve this answer
























      up vote
      0
      down vote













      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





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered May 16 at 8:51









        U880D

        401314




        401314



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405822%2fhow-to-compare-dates-line-by-line-and-get-a-date-range%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            The Forum (Inglewood, California)

            Palaiologos