How to check if a list of numbers are between a range of numbers from file

ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.

Exclusive for LQ members, get up to 45% off per month. Click here for more info.

How to check if a list of numbers are between a range of numbers from file

I have two files that looks like file1 and file2 below. (Original files are bigger and contain more columns, but you get the picture...)
The 10 digit numbers are epoch timestamps, and the date corresponds to the first timestamp.

Task: For all epoch timestamps in file1, I want to check in which range they belong to in file2.
Range in file2 is between column 3 and 4.
The output could look something like this :

1553084280 is between A B 1553106952 1553149296 2019-03-20
1553161680 is between A B 1553366710 1553408326 2019-03-23
1553253660 is not between any range in file2
etc...

Can anyone give some hints in how to do this?
I guess it can be done with for loops and if and then scroll through the range, but I don't know where to start....

B A 1553069354 1553102191 2019-03-20
A B 1553106952 1553149296 2019-03-20
B A 1553154911 1553188655 2019-03-21
A B 1553193719 1553235582 2019-03-21
B A 1553241380 1553274996 2019-03-22
A B 1553279511 1553321916 2019-03-22
B A 1553327895 1553361459 2019-03-23
A B 1553366710 1553408326 2019-03-23
B A 1553414000 1553447801 2019-03-24
A B 1553452378 1553494663 2019-03-24

1 B A 1553069354 1553102191 2019-03-20
2 A B 1553106952 1553149296 2019-03-20
3 B A 1553154911 1553188655 2019-03-21
4 A B 1553193719 1553235582 2019-03-21
5 B A 1553241380 1553274996 2019-03-22
6 A B 1553279511 1553321916 2019-03-22
7 B A 1553327895 1553361459 2019-03-23
8 A B 1553366710 1553408326 2019-03-23
9 B A 1553414000 1553447801 2019-03-24
10 A B 1553452378 1553494663 2019-03-24

If the timestamp was 1553249000, how can I get the output to be "timestamp 1553249000 is in range line 5..." ?

Thanks for your reply.
I'm not very experienced in any programming language, so I would prefer bash scripting, using awk, sed etc, and for/while loops...

B A 1553069354 1553102191 2019-03-20
A B 1553106952 1553149296 2019-03-20
B A 1553154911 1553188655 2019-03-21
A B 1553193719 1553235582 2019-03-21
B A 1553241380 1553274996 2019-03-22
A B 1553279511 1553321916 2019-03-22
B A 1553327895 1553361459 2019-03-23
A B 1553366710 1553408326 2019-03-23
B A 1553414000 1553447801 2019-03-24
A B 1553452378 1553494663 2019-03-24

B A 1553069354 1553102191 2019-03-20
1553075280 2019-03-20 50
1553084280 2019-03-20 50
A B 1553106952 1553149296 2019-03-20
B A 1553154911 1553188655 2019-03-21
1553161680 2019-03-21 50
A B 1553193719 1553235582 2019-03-21
B A 1553241380 1553274996 2019-03-22
1553253660 2019-03-22 50
1553265360 2019-03-22 50
1553278380 2019-03-22 50
A B 1553279511 1553321916 2019-03-22
1553285220 2019-03-22 50
B A 1553327895 1553361459 2019-03-23
A B 1553366710 1553408326 2019-03-23
B A 1553414000 1553447801 2019-03-24
A B 1553452378 1553494663 2019-03-24
1553461200 2019-03-24 50
1553533680 2019-03-25 50
1553563800 2019-03-26 50

Daniel B. Martin

.

Last edited by danielbmartin; 03-28-2019 at 05:07 PM.
Reason: Tighten the code, slightly

B A 1553069354 1553102191 2019-03-20
A B 1553106952 1553149296 2019-03-20
B A 1553154911 1553188655 2019-03-21
A B 1553193719 1553235582 2019-03-21
B A 1553241380 1553274996 2019-03-22
A B 1553279511 1553321916 2019-03-22
B A 1553327895 1553361459 2019-03-23
A B 1553366710 1553408326 2019-03-23
B A 1553414000 1553447801 2019-03-24
A B 1553452378 1553494663 2019-03-24

... this awk ...

Code:

awk '{if (NF==3) a[j++]=$1" "
else {a[j++]=$3" "$0
a[j++]=$4" end"}}
END{n=asort(a,b)
for (k=1;k<=n;k++)
{l=length(b[k])
if (l<12 && flag) print b[k]"is within",Range
if (l<12 && !flag) print b[k]"is outside any range"
if (l>12) {flag=!flag;
Range=substr(b[k],1+index(b[k]," "))}}}' \
$InFile1 $InFile2 >$OutFile

... produced this OutFile ...

Code:

1553075280 is within B A 1553069354 1553102191 2019-03-20
1553084280 is within B A 1553069354 1553102191 2019-03-20
1553161680 is within B A 1553154911 1553188655 2019-03-21
1553253660 is within B A 1553241380 1553274996 2019-03-22
1553265360 is within B A 1553241380 1553274996 2019-03-22
1553278380 is outside any range
1553285220 is within A B 1553279511 1553321916 2019-03-22
1553461200 is within A B 1553452378 1553494663 2019-03-24
1553533680 is outside any range
1553563800 is outside any range

The awk could be made (slightly) more efficient by coding nested if statements but it would be less readable.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.