Bash: compare target date to series of dates
up vote
0
down vote
favorite
Given a target date and a set of two columns (the first being a random number and the second a date in order from most recent to oldest), I want to find the number for the row with the date closest to but earlier than the target. For example, given the following:
target: 2018-12-03 19:09:56.250641
columns:
5346 2018-12-06 17:44:35.010724
6347 2018-12-05 17:50:46.475593
7284 2018-12-04 18:32:11.665405
0298 2018-12-02 22:28:04.59453
1836 2018-12-02 22:27:47.585642
6653 2018-12-02 21:26:13.942103
9274 2018-12-02 21:23:28.318704
I would want to return 0298. What is the cleanest way to do this?
text-processing date
New contributor
Wmbuch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
Given a target date and a set of two columns (the first being a random number and the second a date in order from most recent to oldest), I want to find the number for the row with the date closest to but earlier than the target. For example, given the following:
target: 2018-12-03 19:09:56.250641
columns:
5346 2018-12-06 17:44:35.010724
6347 2018-12-05 17:50:46.475593
7284 2018-12-04 18:32:11.665405
0298 2018-12-02 22:28:04.59453
1836 2018-12-02 22:27:47.585642
6653 2018-12-02 21:26:13.942103
9274 2018-12-02 21:23:28.318704
I would want to return 0298. What is the cleanest way to do this?
text-processing date
New contributor
Wmbuch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Given a target date and a set of two columns (the first being a random number and the second a date in order from most recent to oldest), I want to find the number for the row with the date closest to but earlier than the target. For example, given the following:
target: 2018-12-03 19:09:56.250641
columns:
5346 2018-12-06 17:44:35.010724
6347 2018-12-05 17:50:46.475593
7284 2018-12-04 18:32:11.665405
0298 2018-12-02 22:28:04.59453
1836 2018-12-02 22:27:47.585642
6653 2018-12-02 21:26:13.942103
9274 2018-12-02 21:23:28.318704
I would want to return 0298. What is the cleanest way to do this?
text-processing date
New contributor
Wmbuch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Given a target date and a set of two columns (the first being a random number and the second a date in order from most recent to oldest), I want to find the number for the row with the date closest to but earlier than the target. For example, given the following:
target: 2018-12-03 19:09:56.250641
columns:
5346 2018-12-06 17:44:35.010724
6347 2018-12-05 17:50:46.475593
7284 2018-12-04 18:32:11.665405
0298 2018-12-02 22:28:04.59453
1836 2018-12-02 22:27:47.585642
6653 2018-12-02 21:26:13.942103
9274 2018-12-02 21:23:28.318704
I would want to return 0298. What is the cleanest way to do this?
text-processing date
text-processing date
New contributor
Wmbuch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Wmbuch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
Jeff Schaller
37.5k1052121
37.5k1052121
New contributor
Wmbuch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 days ago
Wmbuch
31
31
New contributor
Wmbuch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Wmbuch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Wmbuch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Since the dates are in Y-M-D H:M:S order, they could be compared as text (lexicographical or dictionary order in layman terms).
There are three fields (columns), not two. Unless the delimiter for the first column is different than the delimiter for the other columns. As you present the delimiters as space there is no way for us to know if it is an space or a tab or something else. I'll assume three columns delimited by either space or tabs.
To solve this issue, set a variable with the value of the search date and use this command:
s='2018-12-03 19:09:56.250641'
awk -vs="$s" '( $2" "$3 < s ){ print $1; exit }' infile
That is:
- Compare the concatenated values of fields 2 and 3 with the value searched.
- When this comparison becomes true (lower)
- Print the first column and exit.
Works perfectly! I'm not too familiar withawksyntax. Would you mind explaining what$2" "$3 < sis doing? I get that you're comparing something to thesvariable, but I'm unsure what parts of the input are represented by$2and$3
– Wmbuch
2 days ago
Second ($2) and third ($3) column?.
– Isaac
2 days ago
Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reasonawktreated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
– Wmbuch
2 days ago
add a comment |
up vote
0
down vote
I normally would do this with awk as well, but here's a pipe of text tools to achieve the same result:
$ echo "0000 $s" | sort -k2 - infile | grep -B1 "$s" | head -1 | cut -d" " -f1
0298
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Since the dates are in Y-M-D H:M:S order, they could be compared as text (lexicographical or dictionary order in layman terms).
There are three fields (columns), not two. Unless the delimiter for the first column is different than the delimiter for the other columns. As you present the delimiters as space there is no way for us to know if it is an space or a tab or something else. I'll assume three columns delimited by either space or tabs.
To solve this issue, set a variable with the value of the search date and use this command:
s='2018-12-03 19:09:56.250641'
awk -vs="$s" '( $2" "$3 < s ){ print $1; exit }' infile
That is:
- Compare the concatenated values of fields 2 and 3 with the value searched.
- When this comparison becomes true (lower)
- Print the first column and exit.
Works perfectly! I'm not too familiar withawksyntax. Would you mind explaining what$2" "$3 < sis doing? I get that you're comparing something to thesvariable, but I'm unsure what parts of the input are represented by$2and$3
– Wmbuch
2 days ago
Second ($2) and third ($3) column?.
– Isaac
2 days ago
Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reasonawktreated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
– Wmbuch
2 days ago
add a comment |
up vote
2
down vote
accepted
Since the dates are in Y-M-D H:M:S order, they could be compared as text (lexicographical or dictionary order in layman terms).
There are three fields (columns), not two. Unless the delimiter for the first column is different than the delimiter for the other columns. As you present the delimiters as space there is no way for us to know if it is an space or a tab or something else. I'll assume three columns delimited by either space or tabs.
To solve this issue, set a variable with the value of the search date and use this command:
s='2018-12-03 19:09:56.250641'
awk -vs="$s" '( $2" "$3 < s ){ print $1; exit }' infile
That is:
- Compare the concatenated values of fields 2 and 3 with the value searched.
- When this comparison becomes true (lower)
- Print the first column and exit.
Works perfectly! I'm not too familiar withawksyntax. Would you mind explaining what$2" "$3 < sis doing? I get that you're comparing something to thesvariable, but I'm unsure what parts of the input are represented by$2and$3
– Wmbuch
2 days ago
Second ($2) and third ($3) column?.
– Isaac
2 days ago
Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reasonawktreated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
– Wmbuch
2 days ago
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Since the dates are in Y-M-D H:M:S order, they could be compared as text (lexicographical or dictionary order in layman terms).
There are three fields (columns), not two. Unless the delimiter for the first column is different than the delimiter for the other columns. As you present the delimiters as space there is no way for us to know if it is an space or a tab or something else. I'll assume three columns delimited by either space or tabs.
To solve this issue, set a variable with the value of the search date and use this command:
s='2018-12-03 19:09:56.250641'
awk -vs="$s" '( $2" "$3 < s ){ print $1; exit }' infile
That is:
- Compare the concatenated values of fields 2 and 3 with the value searched.
- When this comparison becomes true (lower)
- Print the first column and exit.
Since the dates are in Y-M-D H:M:S order, they could be compared as text (lexicographical or dictionary order in layman terms).
There are three fields (columns), not two. Unless the delimiter for the first column is different than the delimiter for the other columns. As you present the delimiters as space there is no way for us to know if it is an space or a tab or something else. I'll assume three columns delimited by either space or tabs.
To solve this issue, set a variable with the value of the search date and use this command:
s='2018-12-03 19:09:56.250641'
awk -vs="$s" '( $2" "$3 < s ){ print $1; exit }' infile
That is:
- Compare the concatenated values of fields 2 and 3 with the value searched.
- When this comparison becomes true (lower)
- Print the first column and exit.
answered 2 days ago
Isaac
10.8k11447
10.8k11447
Works perfectly! I'm not too familiar withawksyntax. Would you mind explaining what$2" "$3 < sis doing? I get that you're comparing something to thesvariable, but I'm unsure what parts of the input are represented by$2and$3
– Wmbuch
2 days ago
Second ($2) and third ($3) column?.
– Isaac
2 days ago
Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reasonawktreated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
– Wmbuch
2 days ago
add a comment |
Works perfectly! I'm not too familiar withawksyntax. Would you mind explaining what$2" "$3 < sis doing? I get that you're comparing something to thesvariable, but I'm unsure what parts of the input are represented by$2and$3
– Wmbuch
2 days ago
Second ($2) and third ($3) column?.
– Isaac
2 days ago
Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reasonawktreated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
– Wmbuch
2 days ago
Works perfectly! I'm not too familiar with
awk syntax. Would you mind explaining what $2" "$3 < s is doing? I get that you're comparing something to the s variable, but I'm unsure what parts of the input are represented by $2 and $3– Wmbuch
2 days ago
Works perfectly! I'm not too familiar with
awk syntax. Would you mind explaining what $2" "$3 < s is doing? I get that you're comparing something to the s variable, but I'm unsure what parts of the input are represented by $2 and $3– Wmbuch
2 days ago
Second (
$2) and third ($3) column?.– Isaac
2 days ago
Second (
$2) and third ($3) column?.– Isaac
2 days ago
Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reason
awk treated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P– Wmbuch
2 days ago
Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reason
awk treated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P– Wmbuch
2 days ago
add a comment |
up vote
0
down vote
I normally would do this with awk as well, but here's a pipe of text tools to achieve the same result:
$ echo "0000 $s" | sort -k2 - infile | grep -B1 "$s" | head -1 | cut -d" " -f1
0298
add a comment |
up vote
0
down vote
I normally would do this with awk as well, but here's a pipe of text tools to achieve the same result:
$ echo "0000 $s" | sort -k2 - infile | grep -B1 "$s" | head -1 | cut -d" " -f1
0298
add a comment |
up vote
0
down vote
up vote
0
down vote
I normally would do this with awk as well, but here's a pipe of text tools to achieve the same result:
$ echo "0000 $s" | sort -k2 - infile | grep -B1 "$s" | head -1 | cut -d" " -f1
0298
I normally would do this with awk as well, but here's a pipe of text tools to achieve the same result:
$ echo "0000 $s" | sort -k2 - infile | grep -B1 "$s" | head -1 | cut -d" " -f1
0298
answered 2 days ago
RudiC
3,7351312
3,7351312
add a comment |
add a comment |
Wmbuch is a new contributor. Be nice, and check out our Code of Conduct.
Wmbuch is a new contributor. Be nice, and check out our Code of Conduct.
Wmbuch is a new contributor. Be nice, and check out our Code of Conduct.
Wmbuch is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f486717%2fbash-compare-target-date-to-series-of-dates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown