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

Clash 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
linux bash date file-comparison
add a comment |Â
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
linux bash date file-comparison
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
add a comment |Â
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
linux bash date file-comparison
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
linux bash date file-comparison
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
add a comment |Â
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
add a comment |Â
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
==========
The problem with creating simple sequences is that you will also the values from20171132to20171200, which are not valid dates.
â U880D
May 16 at 8:17
add a comment |Â
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
add a comment |Â
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
==========
The problem with creating simple sequences is that you will also the values from20171132to20171200, which are not valid dates.
â U880D
May 16 at 8:17
add a comment |Â
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
==========
The problem with creating simple sequences is that you will also the values from20171132to20171200, which are not valid dates.
â U880D
May 16 at 8:17
add a comment |Â
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
==========
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
==========
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 from20171132to20171200, which are not valid dates.
â U880D
May 16 at 8:17
add a comment |Â
The problem with creating simple sequences is that you will also the values from20171132to20171200, 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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered May 16 at 8:51
U880D
401314
401314
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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