awk matching script not working as expected

This is my ubuntu version:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.4 LTS
Release:    16.04
Codename:   xenial

$ /bin/awk -V | head -n1
bash: /bin/awk: No such file or directory

I have gotten a script that helps me to parse, read, and output the data in a specific format, over on (Unix and Linux) Ubuntu 16. Although I am not getting any errors but the script is NOT producing the result that I expect.

What the script does? I have a large input data file data.txt (see below); for every timestamp, I need to find the nearest time match 5 minutes later and the value on column 4 and then print both in two new columns.
The logic for the match is - Calculate the time 5 minutes later from the current time stamp, now get the value closes to this time within a 2 mins limit in either direction, for example lets say time now is 00:00:00.000 get the value no sooner that 00:03:00.000 and no later than 00:07:00.000, else print NA NA. Not to mention that the final matches will all show NAs.

for example for

 2018-02-16 16:45:29.557 farads 0.0004300000 

I need to find the best time available 5 minutes later (i. e. approximately)

 16:50:40.486 0.0002400000 

So the output line must be

2018-02-16 16:45:29.557 farads 0.0004300000 16:50:40.486 0.0002400000 

Please note that there are no exact time matches here, that is why only the nearest match is found. Also the code should seamlessly work when the date changes from 2018-02-16 to 2018-02-17.

However, this script is not outputting things properly. The expected output should look like this

 2018-02-16 16:42:53.926 farads 0.0000000000 2018-02-16 16:47:51.612 0.0006500000
 2018-02-16 16:42:53.927 farads 0.0000000000 2018-02-16 16:47:51.612 0.0006500000
 2018-02-16 16:43:33.167 farads 0.0000800000 2018-02-16 16:48:34.077 0.0006600000
 2018-02-16 16:44:21.357 farads 0.0002600000 2018-02-16 16:49:17.015 0.0003300000
 2018-02-16 16:45:29.557 farads 0.0004300000 2018-02-16 16:50:40.486 0.0002400000
 2018-02-16 16:45:29.558 farads 0.0004300000 2018-02-16 16:50:40.486 0.0002400000
 2018-02-16 16:46:09.300 farads 0.0004300000 2018-02-16 16:51:22.525 0.0005900000
 2018-02-16 16:47:10.987 farads 0.0002800000 2018-02-16 16:52:01.997 0.0003900000
 2018-02-16 16:47:51.611 farads 0.0006500000 2018-02-16 16:52:43.612 0.0005200000
 2018-02-16 16:47:51.612 farads 0.0006500000 2018-02-16 16:52:43.612 0.0005200000
 2018-02-16 16:48:34.077 farads 0.0006600000 2018-02-16 16:53:23.550 0.0003900000
 2018-02-16 16:49:17.015 farads 0.0003300000 2018-02-16 16:54:03.276 0.0005300000
 2018-02-16 16:49:59.075 farads 0.0000700000 2018-02-16 16:54:44.223 0.0003800000
 2018-02-16 16:50:40.486 farads 0.0002400000 2018-02-16 16:55:24.769 0.0003200000
 2018-02-16 16:51:22.525 farads 0.0005900000 2018-02-16 16:56:10.028 0.0002700000
 2018-02-16 16:52:01.997 farads 0.0003900000 2018-02-16 16:56:57.624 0.0000900000
 2018-02-16 16:52:43.612 farads 0.0005200000 2018-02-16 16:57:37.387 0.0003000000
 2018-02-16 16:53:23.550 farads 0.0003900000 2018-02-16 16:58:16.929 0.0005800000
 2018-02-16 16:54:03.276 farads 0.0005300000 2018-02-16 16:58:56.961 0.0003000000
 2018-02-16 16:54:44.223 farads 0.0003800000 2018-02-16 16:59:39.217 0.0001900000
 2018-02-16 16:55:24.769 farads 0.0003200000 2018-02-16 17:00:19.129 0.0005800000
 2018-02-16 16:56:10.028 farads 0.0002700000 2018-02-16 17:00:59.328 0.0001500000
 2018-02-16 16:56:57.624 farads 0.0000900000 2018-02-16 17:01:39.138 0.0005400000
 2018-02-16 16:57:37.387 farads 0.0003000000 2018-02-16 17:02:19.786 0.0006600000
 2018-02-16 16:58:16.929 farads 0.0005800000 2018-02-16 17:03:00.236 0.0004700000
 2018-02-16 16:58:56.961 farads 0.0003000000 2018-02-16 17:03:44.343 0.0003300000
 2018-02-16 16:59:39.217 farads 0.0001900000 2018-02-16 17:04:24.996 0.0002200000
 2018-02-16 17:00:19.129 farads 0.0005800000 2018-02-16 17:05:05.754 0.0003200000
 2018-02-16 17:00:59.328 farads 0.0001500000 2018-02-16 17:05:48.512 0.0004600000
 2018-02-16 17:01:39.138 farads 0.0005400000 2018-02-16 17:06:29.248 0.0003700000
 2018-02-16 17:02:19.786 farads 0.0006600000 2018-02-16 17:07:09.819 0.0001300000
 2018-02-16 17:03:00.236 farads 0.0004700000 2018-02-16 17:07:50.392 0.0005500000
 2018-02-16 17:03:44.343 farads 0.0003300000 2018-02-16 17:08:32.397 0.0002000000
 2018-02-16 17:04:24.996 farads 0.0002200000 2018-02-16 17:09:14.778 0.0003000000
 2018-02-16 17:05:05.754 farads 0.0003200000 2018-02-16 17:09:57.688 0.0003100000
 2018-02-16 17:05:48.512 farads 0.0004600000 2018-02-16 17:10:37.237 0.0003900000
 2018-02-16 17:06:29.248 farads 0.0003700000 2018-02-16 17:11:21.559 0.0003500000
 2018-02-16 17:07:09.819 farads 0.0001300000 2018-02-16 17:12:00.946 0.0003500000
 2018-02-16 17:07:50.392 farads 0.0005500000 2018-02-16 17:12:44.127 0.0003200000
 2018-02-16 17:08:32.397 farads 0.0002000000 2018-02-16 17:13:26.579 0.0003800000
 2018-02-16 17:09:14.778 farads 0.0003000000 2018-02-16 17:14:09.175 0.0001100000
 2018-02-16 17:09:57.688 farads 0.0003100000 2018-02-16 17:14:49.553 0.0001300000
 2018-02-16 17:10:37.237 farads 0.0003900000 2018-02-16 17:15:31.044 0.0002000000

This is how I run the script from the command line:

awk -f awkscript3 < del_in | less

output of the following command:

it's giving me this instead: i. e. it prints the four lines of the input file unsorted.

2018-02-16 18:14:04.826 farads 0.0004100000 
2018-02-16 17:49:01.891 farads 0.0001600000 
2018-02-16 17:26:03.886 farads 0.0003100000 
2018-02-16 18:12:41.023 farads 0.0002400000 
2018-02-16 17:41:13.307 farads 0.0001500000 
2018-02-16 17:41:13.308 farads 0.0001500000 
2018-02-16 17:25:23.026 farads 0.0001400000 
2018-02-16 17:06:29.248 farads 0.0003700000 
2018-02-16 18:23:46.638 farads 0.0002100000 
2018-02-16 17:36:22.164 farads 0.0002600000 
2018-02-16 18:15:31.247 farads 0.0003500000 
2018-02-16 17:42:33.994 farads 0.0002300000 
2018-02-16 16:54:44.223 farads 0.0003800000 
2018-02-16 18:25:52.344 farads 0.0003000000 
2018-02-16 17:17:33.238 farads 0.0001900000 
2018-02-16 18:33:52.725 farads 0.0002300000 
2018-02-16 17:53:14.374 farads 0.0003300000 
2018-02-16 17:21:43.477 farads 0.0002000000 
2018-02-16 17:10:37.237 farads 0.0003900000 
2018-02-16 18:10:41.062 farads 0.0001900000 
2018-02-16 17:54:34.453 farads 0.0001200000 
2018-02-16 17:32:11.586 farads 0.0003800000

Here is the input data file data.txt:

2018-02-16 16:45:29.557 farads 0.0004300000
2018-02-16 16:46:09.300 farads 0.0004300000
2018-02-16 16:47:10.987 farads 0.0002800000
2018-02-16 16:47:51.611 farads 0.0006500000
2018-02-16 16:47:51.612 farads 0.0006500000
2018-02-16 16:48:34.077 farads 0.0006600000
2018-02-16 16:49:17.015 farads 0.0003300000
2018-02-16 16:49:59.075 farads 0.0000700000
2018-02-16 16:50:40.486 farads 0.0002400000
2018-02-16 16:51:22.525 farads 0.0005900000
2018-02-16 16:52:01.997 farads 0.0003900000
2018-02-16 16:52:43.612 farads 0.0005200000
2018-02-16 16:53:23.550 farads 0.0003900000
2018-02-16 16:54:03.276 farads 0.0005300000
2018-02-16 16:54:44.223 farads 0.0003800000
2018-02-16 16:55:24.769 farads 0.0003200000
2018-02-16 16:56:10.028 farads 0.0002700000
2018-02-16 16:56:57.624 farads 0.0000900000
2018-02-16 16:57:37.387 farads 0.0003000000
2018-02-16 16:58:16.929 farads 0.0005800000
2018-02-16 16:58:56.961 farads 0.0003000000
2018-02-16 16:59:39.217 farads 0.0001900000
2018-02-16 17:00:19.129 farads 0.0005800000
2018-02-16 17:00:59.328 farads 0.0001500000
2018-02-16 17:01:39.138 farads 0.0005400000
2018-02-16 17:02:19.786 farads 0.0006600000
2018-02-16 17:03:00.236 farads 0.0004700000
2018-02-16 17:03:44.343 farads 0.0003300000
2018-02-16 17:04:24.996 farads 0.0002200000
2018-02-16 17:05:05.754 farads 0.0003200000
2018-02-16 17:05:48.512 farads 0.0004600000
2018-02-16 17:06:29.248 farads 0.0003700000
2018-02-16 17:07:09.819 farads 0.0001300000
2018-02-16 17:07:50.392 farads 0.0005500000
2018-02-16 17:08:32.397 farads 0.0002000000
2018-02-16 17:09:14.778 farads 0.0003000000
2018-02-16 17:09:57.688 farads 0.0003100000
2018-02-16 17:10:37.237 farads 0.0003900000
2018-02-16 17:11:21.559 farads 0.0003500000
2018-02-16 17:12:00.945 farads 0.0003500000
2018-02-16 17:12:00.946 farads 0.0003500000
2018-02-16 17:12:44.127 farads 0.0003200000
2018-02-16 17:13:26.579 farads 0.0003800000
2018-02-16 17:14:09.175 farads 0.0001100000
2018-02-16 17:14:49.552 farads 0.0001300000
2018-02-16 17:14:49.553 farads 0.0001300000
2018-02-16 17:15:31.044 farads 0.0002000000
2018-02-16 17:16:12.038 farads 0.0000400000
2018-02-16 17:16:12.039 farads 0.0000400000
2018-02-16 17:16:52.956 farads 0.0002300000
2018-02-16 17:17:33.238 farads 0.0001900000
2018-02-16 17:18:12.986 farads 0.0001900000
2018-02-16 17:18:12.987 farads 0.0001900000
2018-02-16 17:18:54.214 farads 0.0002300000
2018-02-16 17:19:34.432 farads 0.0001500000
2018-02-16 17:20:22.002 farads 0.0000700000
2018-02-16 17:21:03.320 farads 0.0001100000
2018-02-16 17:21:43.477 farads 0.0002000000
2018-02-16 17:22:27.400 farads 0.0003500000
2018-02-16 17:23:11.224 farads 0.0001700000
2018-02-16 17:23:52.907 farads 0.0001100000
2018-02-16 17:24:40.392 farads 0.0001500000
2018-02-16 17:25:23.026 farads 0.0001400000
2018-02-16 17:26:03.886 farads 0.0003100000
2018-02-16 17:26:45.191 farads 0.0001900000
2018-02-16 17:26:45.192 farads 0.0001900000
2018-02-16 17:27:28.652 farads 0.0001000000
2018-02-16 17:28:09.625 farads 0.0002000000
2018-02-16 17:28:49.753 farads 0.0001500000
2018-02-16 17:29:29.224 farads 0.0005600000
2018-02-16 17:30:10.520 farads 0.0002100000
2018-02-16 17:30:50.702 farads 0.0001700000
2018-02-16 17:31:30.077 farads 0.0004800000
2018-02-16 17:32:11.586 farads 0.0003800000
2018-02-16 17:32:51.434 farads 0.0003600000
2018-02-16 17:33:31.457 farads 0.0005300000
2018-02-16 17:34:10.910 farads 0.0007600000
2018-02-16 17:34:51.174 farads 0.0004400000
2018-02-16 17:34:51.175 farads 0.0004400000
2018-02-16 17:35:31.234 farads 0.0004800000
2018-02-16 17:36:22.164 farads 0.0002600000
2018-02-16 17:37:02.616 farads 0.0004100000
2018-02-16 17:37:42.127 farads 0.0003500000
2018-02-16 17:38:23.346 farads 0.0004100000
2018-02-16 17:39:04.611 farads 0.0002400000
2018-02-16 17:39:46.119 farads 0.0000700000
2018-02-16 17:40:33.633 farads 0.0001500000
2018-02-16 17:41:13.307 farads 0.0001500000
2018-02-16 17:41:13.308 farads 0.0001500000
2018-02-16 17:41:54.643 farads 0.0003100000
2018-02-16 17:42:33.994 farads 0.0002300000
2018-02-16 17:43:14.389 farads 0.0004000000
2018-02-16 17:43:54.324 farads 0.0002200000
2018-02-16 17:44:36.122 farads 0.0001800000
2018-02-16 17:45:16.828 farads 0.0002600000
2018-02-16 17:45:59.158 farads 0.0001500000
2018-02-16 17:46:45.126 farads 0.0000700000
2018-02-16 17:47:25.236 farads 0.0004400000
2018-02-16 17:48:12.096 farads 0.0002200000
2018-02-16 17:49:01.891 farads 0.0001600000
2018-02-16 17:49:50.422 farads 0.0002100000
2018-02-16 17:50:31.222 farads 0.0001300000
2018-02-16 17:51:12.651 farads 0.0002600000
2018-02-16 17:51:12.652 farads 0.0002600000
2018-02-16 17:51:53.478 farads 0.0001300000
2018-02-16 17:52:34.145 farads 0.0004600000
2018-02-16 17:53:14.374 farads 0.0003300000
2018-02-16 23:53:53.906 farads 0.0002600000
2018-02-16 23:54:34.453 farads 0.0001200000
2018-02-16 23:55:15.512 farads 0.0001600000
2018-02-16 23:55:58.161 farads 0.0001800000
2018-02-16 23:56:46.602 farads 0.0002600000
2018-02-16 23:57:26.829 farads 0.0003100000
2018-02-16 23:57:26.830 farads 0.0003100000
2018-02-16 23:58:06.991 farads 0.0004400000
2018-02-16 23:58:47.104 farads 0.0003600000
2018-02-16 23:58:47.105 farads 0.0003600000
2018-02-16 23:59:27.080 farads 0.0002300000
2018-02-17 00:00:07.008 farads 0.0002900000
2018-02-17 00:00:07.009 farads 0.0002900000
2018-02-17 00:00:51.685 farads 0.0004900000
2018-02-17 00:01:30.835 farads 0.0003000000
2018-02-17 00:02:14.187 farads 0.0004300000
2018-02-17 00:02:56.048 farads 0.0004700000
2018-02-17 00:03:39.758 farads 0.0004200000
2018-02-17 00:04:19.990 farads 0.0001600000
2018-02-17 00:04:59.854 farads 0.0001700000
2018-02-17 00:05:40.967 farads 0.0001400000
2018-02-17 00:06:24.584 farads 0.0001000000
2018-02-17 00:07:04.742 farads 0.0002500000
2018-02-17 00:07:48.107 farads 0.0003600000
2018-02-17 00:08:31.136 farads 0.0000700000
2018-02-17 00:09:12.429 farads 0.0001500000
2018-02-17 00:09:59.567 farads 0.0002500000
2018-02-17 00:10:41.062 farads 0.0001900000
2018-02-17 00:11:21.016 farads 0.0001600000
2018-02-17 00:12:00.863 farads 0.0001600000
2018-02-17 00:12:41.023 farads 0.0002400000
2018-02-17 00:13:22.429 farads 0.0001500000
2018-02-17 00:14:04.826 farads 0.0004100000
2018-02-17 00:14:51.079 farads 0.0001600000
2018-02-17 00:15:31.247 farads 0.0003500000
2018-02-17 00:16:17.396 farads 0.0001900000
2018-02-17 00:16:56.912 farads 0.0002100000
2018-02-17 00:17:37.895 farads 0.0001800000
2018-02-17 00:18:18.354 farads 0.0003700000
2018-02-17 00:18:58.071 farads 0.0004700000
2018-02-17 18:19:38.135 farads 0.0002000000
2018-02-17 18:20:22.373 farads 0.0002600000
2018-02-17 18:21:02.161 farads 0.0003000000
2018-02-17 18:21:43.806 farads 0.0002700000
2018-02-17 18:22:25.394 farads 0.0002500000
2018-02-17 18:23:06.549 farads 0.0003100000
2018-02-17 18:23:46.638 farads 0.0002100000
2018-02-17 18:24:27.966 farads 0.0001800000
2018-02-17 18:25:11.832 farads 0.0002800000
2018-02-17 18:25:52.344 farads 0.0003000000
2018-02-17 18:26:33.672 farads 0.0002600000
2018-02-17 18:27:15.499 farads 0.0004300000
2018-02-17 18:27:55.288 farads 0.0004800000
2018-02-17 18:28:56.699 farads 0.0004200000
2018-02-17 18:29:40.909 farads 0.0002100000
2018-02-17 18:30:20.942 farads 0.0003400000
2018-02-17 18:31:03.937 farads 0.0003500000
2018-02-17 18:31:51.329 farads 0.0002500000
2018-02-17 18:32:32.608 farads 0.0005000000
2018-02-17 18:33:12.869 farads 0.0004900000
2018-02-17 18:33:52.725 farads 0.0002300000
2018-02-17 18:34:39.022 farads 0.0001300000
2018-02-17 18:35:20.579 farads 0.0002800000
2018-02-17 18:36:00.487 farads 0.0002400000
2018-02-17 18:36:51.908 farads 0.0004500000
2018-02-17 18:37:33.667 farads 0.0002500000
2018-02-17 18:38:13.989 farads 0.0004700000
2018-02-17 18:38:53.753 farads 0.0003500000
2018-02-17 18:39:34.052 farads 0.0004100000

here is the script:

#!usr/bin/awk

{
  day=$1
  split($2, t, ".")
  x=day " " t[1]
  ("date --date=\""x"\" +%s") | getline seconds
  close("date --date=\""x"\" +%s")

  t[3]=seconds"."t[2]
  records[t[3]]=$0
}
 END {
      for (record in records) {
      min=(0 + "INF")
      for (others in records) {
       # skip myself
      if (others == record) continue
      difference=(others - record) - (5 * 60)
      if (difference < 0) difference=-difference
      if (difference < min) {
      min=difference
      matchfor[record]=records[others]
      }
    }
       if (min > (60*2)) {
       print records[record], "NA NA"
    }  else {
       sub("farads", " ", matchfor[record])
       print records[record], matchfor[record]
    }
  }
}

DEBUG MODE OUTPUT:gawk --lint -f awkscript3 < data.txt

awk: awkscript3:29: (FILENAME=- FNR=183) warning: reference to 
uninitialized element `matchfor["1518802491.175"]'
gawk: awkscript3:29: (FILENAME=- FNR=183) warning: reference to 
uninitialized element `matchfor["1518804802.429"]'
gawk: awkscript3:29: (FILENAME=- FNR=183) warning: reference to 
uninitialized element `matchfor["1518801331.044"]'
gawk: awkscript3:29: (FILENAME=- FNR=183) warning: reference to 
uninitialized element `matchfor["1518799413.167"]'

Can the experts please help

#!usr/bin/awk

{
  day=$1
  split($2, t, ".")
  x=day " " t[1]
  ("date --date=\""x"\" +%s") | getline seconds
  close("date --date=\""x"\" +%s")

  t[3]=seconds"."t[2]
  records[t[3]]=$0
}
 END {
    for (record in records) {
       #min=(0 + "INF")
       lmin=0
       for (others in records) {
          # skip myself
          if (others == record) continue
             difference=(others - record) - (5 * 60)
             if (difference < 0) difference=-difference
             if (!lmin++) min=difference + 1
             if (difference < min) {
                min=difference
                matchfor[record]=records[others]
             }
       }
       if (min > (60*2)) {
          print records[record], "NA NA"
       } else {
          sub("farads", " ", matchfor[record])
          print records[record], matchfor[record]
       }
   }
}

your proposed changes

if (!lmin++) lmin=0 and min=difference + 1 

as well as Replacing

min=(0+"inf") WITH min="inf"

is printing only "NA NA" in the last two columns it's not matching anything

2018-02-16 17:02:19.786 farads 0.0006600000 NA NA
2018-02-16 17:03:44.343 farads 0.0003300000 NA NA
2018-02-16 17:04:24.996 farads 0.0002200000 NA NA
2018-02-16 17:05:48.512 farads 0.0004600000 NA NA
2018-02-16 17:06:29.248 farads 0.0003700000 NA NA
2018-02-16 17:20:22.002 farads 0.0000700000 NA NA
2018-02-16 17:07:50.392 farads 0.0005500000 NA NA
2018-02-16 17:21:43.477 farads 0.0002000000 NA NA
2018-02-16 17:08:32.397 farads 0.0002000000 NA NA
2018-02-16 17:22:27.400 farads 0.0003500000 NA NA
2018-02-16 17:09:57.688 farads 0.0003100000 NA NA
2018-02-16 17:23:52.907 farads 0.0001100000 NA NA
2018-02-16 17:24:40.392 farads 0.0001500000 NA NA
2018-02-16 17:25:23.026 farads 0.0001400000 NA NA
2018-02-16 17:26:45.192 farads 0.0001900000 NA NA
2018-02-16 17:40:33.633 farads 0.0001500000 NA NA

I request you run the script in your Ubuntu or UNIX machine in debug mode and see if you can replicate the results.

Are you sure your script is starting at all?

What is "approximately 5 minutes later"? Within 5 min? Beyond 5 min? 10 sec / 30 sec / 100 sec within / beyond?

Why the 612 ms, not the 611 ms entry?

You shebang might not work as it seems to be a relative path.

Is this ms precision to eliminate duplicate indices? ms wouldn't make sense if you're dealing with approximate 5 min?

With min = 0 and difference ALWAYS positive, the if branch will NEVER be entered. So - no surprise matchfor is undefined / uninitialized:

Would you mind to take a step back and rephrase your requirements? Like "For any given record take its time stamp, add five minutes, and from there find the line in the data file with the smallest absolute delta time and print it as a suffix to the actual line"

@RudiC I have rephrased the question above. Please see the logic of the match. Basically there is 2 minutes time window on either side to find a match, if none found it prints NAs

To your question weather the code runs- the answer is absolutely yes the code runs. May I request to you copy and paste the code as well as the data.txt file in your ubuntu terminal and run it to see if the results are replicated.

You could try this script. I believe your OS should come with GNU awk by default so it's much more efficient to use the mktime() feature to convert these times.

#!/usr/bin/awk -f
{
  day=$1
  gsub(/-/, " ", day)
  split($2, t, ".")
  gsub(/:/, " ", t[1])
  x=mktime(day " " t[1]) + t[2] / 1000
  rectime[NR]=x
  records[NR]=$0
}

END {
   offset=5*60
   max=2*60
   for (rec in rectime) {
       cur=rec + 1
       target = rectime[rec] + offset
       offsetmin = target - max
       offsetmax = target + max
       best = 9999999
       found = 0
       while(cur in rectime && rectime[cur] < offsetmax) {
           if (rectime[cur] < target && rectime[cur] > offsetmin &&
               best > target - rectime[cur]) {
                  best= target - rectime[cur]
                  found=cur
           }
           if (rectime[cur] >= target) { 
              if(best > rectime[cur] - target) {
                  best=rectime[cur] - target
                  found=cur
               }
               break
           }
           cur++
        }

        if (found)
           print records[rec] " " records[found]
        else
           print records[rec] " NA NA"
    }
}
1 Like

Thanks your code worked. I have a follow up question on your code

Suppose I have the data that needs to be matched in file1
and the data from which match will happen in file2

how will the modified code with input file names and two sets modified variables look like?

for example the data that requires matching is in file1

2018-02-16 16:45:29.557 farads 0.0004300000
2018-02-16 16:46:09.300 farads 0.0004300000
2018-02-16 16:47:10.987 farads 0.0002800000
2018-02-16 16:47:51.611 farads 0.0006500000
2018-02-16 16:47:51.612 farads 0.0006500000
2018-02-16 16:48:34.077 farads 0.0006600000
2018-02-16 16:49:17.015 farads 0.0003300000
2018-02-16 16:49:59.075 farads 0.0000700000
2018-02-16 16:50:40.486 farads 0.0002400000
2018-02-16 16:51:22.525 farads 0.0005900000
2018-02-16 16:52:01.997 farads 0.0003900000
2018-02-16 16:52:43.612 farads 0.0005200000
2018-02-16 16:53:23.550 farads 0.0003900000
2018-02-16 16:54:03.276 farads 0.0005300000

The data from which matching is to be done is in file2

2018-02-16 16:47:51.611 reactance 0.0006500000
2018-02-16 16:47:51.612 reactance 0.0006500000
2018-02-16 16:48:34.077 reactance 0.0006600000
2018-02-16 16:49:17.015 reactance 0.0003300000
2018-02-16 16:49:59.075 reactance 0.0000700000
2018-02-16 16:50:40.486 reactance 0.0002400000
2018-02-16 16:51:22.525 reactance 0.0005900000
2018-02-16 16:52:01.997 reactance 0.0003900000
2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:54:03.276 reactance 0.0005300000
2018-02-16 16:54:44.223 reactance 0.0003800000
2018-02-16 16:55:24.769 reactance 0.0003200000
2018-02-16 16:56:10.028 reactance 0.0002700000
2018-02-16 16:56:57.624 reactance 0.0000900000
2018-02-16 16:57:37.387 reactance 0.0003000000
2018-02-16 16:58:16.929 reactance 0.0005800000
2018-02-16 16:58:56.961 reactance 0.0003000000
2018-02-16 16:59:39.217 reactance 0.0001900000
2018-02-16 17:00:19.129 reactance 0.0005800000
2018-02-16 17:00:59.328 reactance 0.0001500000
2018-02-16 17:01:39.138 reactance 0.0005400000
2018-02-16 17:02:19.786 reactance 0.0006600000
2018-02-16 17:03:00.236 reactance 0.0004700000
2018-02-16 17:03:44.343 reactance 0.0003300000
2018-02-16 17:04:24.996 reactance 0.0002200000
2018-02-16 17:05:05.754 reactance 0.0003200000
2018-02-16 17:05:48.512 reactance 0.0004600000
2018-02-16 17:06:29.248 reactance 0.0003700000
2018-02-16 17:07:09.819 reactance 0.0001300000
2018-02-16 17:07:50.392 reactance 0.0005500000
2018-02-16 17:08:32.397 reactance 0.0002000000
2018-02-16 17:09:14.778 reactance 0.0003000000
2018-02-16 17:09:57.688 reactance 0.0003100000
2018-02-16 17:10:37.237 reactance 0.0003900000
2018-02-16 17:11:21.559 reactance 0.0003500000
2018-02-16 17:12:00.945 reactance 0.0003500000
2018-02-16 17:12:00.946 reactance 0.0003500000

Matched file should look like this

2018-02-16 16:46:09.300 farads 0.0004300000 2018-02-16 16:51:22.525 reactance 0.0005900000
2018-02-16 16:47:10.987 farads 0.0002800000 2018-02-16 16:52:01.997 reactance 0.0003900000
2018-02-16 16:47:51.611 farads 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:47:51.612 farads 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:48:34.077 farads 0.0006600000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:49:17.015 farads 0.0003300000 2018-02-16 16:54:03.276 reactance 0.0005300000
2018-02-16 16:49:59.075 farads 0.0000700000 2018-02-16 16:54:44.223 reactance 0.0003800000
2018-02-16 16:50:40.486 farads 0.0002400000 2018-02-16 16:55:24.769 reactance 0.0003200000
2018-02-16 16:51:22.525 farads 0.0005900000 2018-02-16 16:56:10.028 reactance 0.0002700000
2018-02-16 16:52:01.997 farads 0.0003900000 2018-02-16 16:56:57.624 reactance 0.0000900000
2018-02-16 16:52:43.612 farads 0.0005200000 2018-02-16 16:57:37.387 reactance 0.0003000000
2018-02-16 16:53:23.550 farads 0.0003900000 2018-02-16 16:58:16.929 reactance 0.0005800000
2018-02-16 16:54:03.276 farads 0.0005300000 2018-02-16 16:58:56.961 reactance 0.0003000000
2018-02-16 16:54:44.223 farads 0.0003800000 2018-02-16 16:59:39.217 reactance 0.0001900000
2018-02-16 16:55:24.769 farads 0.0003200000 2018-02-16 17:00:19.129 reactance 0.0005800000
2018-02-16 16:56:10.028 farads 0.0002700000 2018-02-16 17:00:59.328 reactance 0.0001500000
2018-02-16 16:56:57.624 farads 0.0000900000 2018-02-16 17:01:39.138 reactance 0.0005400000
2018-02-16 16:57:37.387 farads 0.0003000000 2018-02-16 17:02:19.786 reactance 0.0006600000

How about this:

#!/usr/bin/awk -f
FNR==1 {file++}
{
  day=$1
  gsub(/-/, " ", day)
  split($2, t, ".")
  gsub(/:/, " ", t[1])
  x=mktime(day " " t[1]) + t[2] / 1000
  if(file==1) srctime[FNR]=x
  else desttime[FNR]=x
  records[file, FNR]=$0
}

END {
   offset=5*60
   max=2*60
   cur=1
   for (rec in srctime) {
       target = srctime[rec] + offset
       offsetmin = target - max
       offsetmax = target + max
       best = 9999999
       found = 0
       while(cur in desttime && desttime[cur] < offsetmax) {
           if (desttime[cur] < target && desttime[cur] > offsetmin &&
               best > target - desttime[cur]) {
                  best= target - desttime[cur]
                  found=cur
           }
           if (desttime[cur] >= target) {
              if(best > desttime[cur] - target) {
                  best=desttime[cur] - target
                  found=cur
               }
               break
           }
           cur++
        }

        if (found)
           print records[1, rec] " " records[2, found]
        else
           print records[1, rec] " NA NA"
    }
}

Result:

2018-02-16 16:45:29.557 farads 0.0004300000 2018-02-16 16:50:40.486 reactance 0.0002400000
2018-02-16 16:46:09.300 farads 0.0004300000 2018-02-16 16:51:22.525 reactance 0.0005900000
2018-02-16 16:47:10.987 farads 0.0002800000 2018-02-16 16:52:01.997 reactance 0.0003900000
2018-02-16 16:47:51.611 farads 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:47:51.612 farads 0.0006500000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:48:34.077 farads 0.0006600000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:49:17.015 farads 0.0003300000 2018-02-16 16:54:03.276 reactance 0.0005300000
2018-02-16 16:49:59.075 farads 0.0000700000 2018-02-16 16:54:44.223 reactance 0.0003800000
2018-02-16 16:50:40.486 farads 0.0002400000 2018-02-16 16:55:24.769 reactance 0.0003200000
2018-02-16 16:51:22.525 farads 0.0005900000 2018-02-16 16:56:10.028 reactance 0.0002700000
2018-02-16 16:52:01.997 farads 0.0003900000 2018-02-16 16:56:57.624 reactance 0.0000900000
2018-02-16 16:52:43.612 farads 0.0005200000 2018-02-16 16:57:37.387 reactance 0.0003000000
2018-02-16 16:53:23.550 farads 0.0003900000 2018-02-16 16:58:16.929 reactance 0.0005800000
2018-02-16 16:54:03.276 farads 0.0005300000 2018-02-16 16:58:56.961 reactance 0.0003000000

Edit: previous solution could miss closer records that are before previous target this should be more accurate:

#!/usr/bin/awk -f
FNR==1 {file++}
{
  day=$1
  gsub(/-/, " ", day)
  split($2, t, ".")
  gsub(/:/, " ", t[1])
  x=mktime(day " " t[1]) + t[2] / 1000
  if(file==1) srctime[FNR]=x
  else desttime[FNR]=x
  records[file, FNR]=$0
}

END {
   offset=5*60
   max=2*60
   deststart=0
   for (rec in srctime) {
       target = srctime[rec] + offset
       offsetmin = target - max
       offsetmax = target + max
       best = 9999999
       found = 0
       cur=deststart+1
       while(cur in desttime && desttime[cur] < offsetmax) {
           if (desttime[cur] < target && desttime[cur] > offsetmin &&
               best > target - desttime[cur]) {
                  if( best = 9999999) deststart = cur
                  best= target - desttime[cur]
                  found=cur
           }
           if (desttime[cur] >= target) {
              if(best > desttime[cur] - target) {
                  best=desttime[cur] - target
                  found=cur
               }
               break
           }
           cur++
        }

        if (found)
           print records[1, rec] " " records[2, found]
        else
           print records[1, rec] " NA NA"
    }
}
1 Like

What command did you use at the command line to run the code?

gawk --lint -f awkscript4  < file1.txt file2.txt | less 

prints the following:

2018-02-16 16:46:09.300 reactance 0.0004300000 2018-02-16 16:51:22.525 reactance 0.0005900000
2018-02-16 16:47:10.987 reactance 0.0002800000 2018-02-16 16:52:01.997 reactance 0.0003900000
2018-02-16 16:47:51.611 reactance 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:47:51.612 reactance 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:48:34.077 reactance 0.0006600000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:49:17.015 reactance 0.0003300000 2018-02-16 16:54:03.276 reactance 0.0005300000
2018-02-16 16:49:59.075 reactance 0.0000700000 2018-02-16 16:54:44.223 reactance 0.0003800000
2018-02-16 16:50:40.486 reactance 0.0002400000 2018-02-16 16:55:24.769 reactance 0.0003200000
2018-02-16 16:51:22.525 reactance 0.0005900000 2018-02-16 16:56:10.028 reactance 0.0002700000
2018-02-16 16:52:01.997 reactance 0.0003900000 2018-02-16 16:56:57.624 reactance 0.0000900000
2018-02-16 16:52:43.612 reactance 0.0005200000 2018-02-16 16:57:37.387 reactance 0.0003000000
2018-02-16 16:53:23.550 reactance 0.0003900000 2018-02-16 16:58:16.929 reactance 0.0005800000
2018-02-16 16:54:03.276 reactance 0.0005300000 2018-02-16 16:58:56.961 reactance 0.0003000000
2018-02-16 16:54:44.223 reactance 0.0003800000 2018-02-16 16:59:39.217 reactance 0.0001900000
2018-02-16 16:55:24.769 reactance 0.0003200000 2018-02-16 17:00:19.129 reactance 0.0005800000
2018-02-16 16:56:10.028 reactance 0.0002700000 2018-02-16 17:00:59.328 reactance 0.0001500000
2018-02-16 16:56:57.624 reactance 0.0000900000 2018-02-16 17:01:39.138 reactance 0.0005400000
2018-02-16 16:57:37.387 reactance 0.0003000000 2018-02-16 17:02:19.786 reactance 0.0006600000
2018-02-16 16:58:16.929 reactance 0.0005800000 2018-02-16 17:03:00.236 reactance 0.0004700000
2018-02-16 16:58:56.961 reactance 0.0003000000 2018-02-16 17:03:44.343 reactance 0.0003300000
2018-02-16 16:59:39.217 reactance 0.0001900000 2018-02-16 17:04:24.996 reactance 0.0002200000
2018-02-16 17:00:19.129 reactance 0.0005800000 2018-02-16 17:05:05.754 reactance 0.0003200000
2018-02-16 17:00:59.328 reactance 0.0001500000 2018-02-16 17:05:48.512 reactance 0.0004600000
2018-02-16 17:01:39.138 reactance 0.0005400000 2018-02-16 17:06:29.248 reactance 0.0003700000
2018-02-16 17:02:19.786 reactance 0.0006600000 2018-02-16 17:07:09.819 reactance 0.0001300000
2018-02-16 17:03:00.236 reactance 0.0004700000 2018-02-16 17:07:50.392 reactance 0.0005500000
2018-02-16 17:03:44.343 reactance 0.0003300000 2018-02-16 17:08:32.397 reactance 0.0002000000
2018-02-16 17:04:24.996 reactance 0.0002200000 2018-02-16 17:09:14.778 reactance 0.0003000000
2018-02-16 17:05:05.754 reactance 0.0003200000 2018-02-16 17:09:57.688 reactance 0.0003100000

Try:

gawk --lint -f awkscript4  file1.txt file2.txt

or if you have the correct hash bang at the top of your script (something like #!/usr/bin/gawk -f ) you can do:

$ chmod 755 awkscript4
$ ./awkscript4 file1.txt file2.txt

There is a bug in the code. I tried your suggestion, What I see is happening is file1 (containing farads) is matching with file1 itself. file 2 (containing reactance) is matching with file 2 itself and both are merging. I want file1 to match with file2 and print the matched values.

This is what is happening with

gawk --lint -f awkscript4  file1.txt file2.txt
 2018-02-17 00:05:40.967 farads 0.0001400000  2018-02-17 00:12:00.863 farads 0.0001600000
 2018-02-17 00:06:24.584 farads 0.0001000000  2018-02-17 00:12:00.863 farads 0.0001600000
 2018-02-17 00:07:04.742 farads 0.0002500000  2018-02-17 00:12:00.863 farads 0.0001600000
 2018-02-17 00:12:00.863 farads 0.0001600000  2018-02-17 00:16:56.912 farads 0.0002100000
 2018-02-17 00:12:41.023 farads 0.0002400000  2018-02-17 00:17:37.895 farads 0.0001800000
 2018-02-17 00:13:22.429 farads 0.0001500000  2018-02-17 00:18:18.354 farads 0.0003700000
 2018-02-17 00:14:04.826 farads 0.0004100000  2018-02-17 00:18:58.071 farads 0.0004700000
 2018-02-17 00:14:51.079 farads 0.0001600000  2018-02-17 00:18:58.071 farads 0.0004700000
 2018-02-17 00:15:31.247 farads 0.0003500000  2018-02-17 00:18:58.071 farads 0.0004700000
 2018-02-17 00:16:17.396 farads 0.0001900000 NA NA
 2018-02-17 00:16:56.912 farads 0.0002100000 NA NA
 2018-02-17 00:17:37.895 farads 0.0001800000 NA NA
 2018-02-17 00:18:18.354 farads 0.0003700000 NA NA
 2018-02-17 00:18:58.071 farads 0.0004700000 NA NA
 2018-02-17 18:19:38.135 farads 0.0002000000  2018-02-17 18:24:27.966 farads 0.0001800000
 2018-02-17 18:20:22.373 farads 0.0002600000  2018-02-17 18:25:11.832 farads 0.0002800000
 2018-02-17 18:21:02.161 farads 0.0003000000  2018-02-17 18:25:52.344 farads 0.0003000000
 2018-02-17 18:21:43.806 farads 0.0002700000  2018-02-17 18:26:33.672 farads 0.0002600000
 2018-02-17 18:22:25.394 farads 0.0002500000  2018-02-17 18:27:15.499 farads 0.0004300000
 2018-02-17 18:23:06.549 farads 0.0003100000  2018-02-17 18:27:55.288 farads 0.0004800000
 2018-02-17 18:23:46.638 farads 0.0002100000  2018-02-17 18:28:56.699 farads 0.0004200000
 2018-02-17 18:24:27.966 farads 0.0001800000  2018-02-17 18:29:40.909 farads 0.0002100000
 2018-02-17 18:25:11.832 farads 0.0002800000  2018-02-17 18:30:20.942 farads 0.0003400000
 2018-02-17 18:25:52.344 farads 0.0003000000  2018-02-17 18:31:03.937 farads 0.0003500000
 2018-02-17 18:26:33.672 farads 0.0002600000  2018-02-17 18:31:51.329 farads 0.0002500000
 2018-02-17 18:27:15.499 farads 0.0004300000  2018-02-17 18:32:32.608 farads 0.0005000000
 2018-02-17 18:27:55.288 farads 0.0004800000  2018-02-17 18:33:12.869 farads 0.0004900000
 2018-02-17 18:28:56.699 farads 0.0004200000  2018-02-17 18:33:52.725 farads 0.0002300000
 2018-02-17 18:29:40.909 farads 0.0002100000  2018-02-17 18:34:39.022 farads 0.0001300000
 2018-02-17 18:30:20.942 farads 0.0003400000  2018-02-17 18:35:20.579 farads 0.0002800000
 2018-02-17 18:31:03.937 farads 0.0003500000  2018-02-17 18:36:00.487 farads 0.0002400000
 2018-02-17 18:31:51.329 farads 0.0002500000  2018-02-17 18:36:51.908 farads 0.0004500000
 2018-02-17 18:32:32.608 farads 0.0005000000  2018-02-17 18:37:33.667 farads 0.0002500000
 2018-02-17 18:33:12.869 farads 0.0004900000  2018-02-17 18:38:13.989 farads 0.0004700000
 2018-02-17 18:33:52.725 farads 0.0002300000  2018-02-17 18:38:53.753 farads 0.0003500000
 2018-02-17 18:34:39.022 farads 0.0001300000  2018-02-17 18:39:34.052 farads 0.0004100000
 2018-02-17 18:35:20.579 farads 0.0002800000  2018-02-17 18:39:34.052 farads 0.0004100000
 2018-02-17 18:36:00.487 farads 0.0002400000  2018-02-17 18:39:34.052 farads 0.0004100000
 2018-02-17 18:36:51.908 farads 0.0004500000 NA NA
 2018-02-17 18:37:33.667 farads 0.0002500000 NA NA
 2018-02-17 18:38:13.989 farads 0.0004700000 NA NA
 2018-02-17 18:38:53.753 farads 0.0003500000 NA NA
 2018-02-17 18:39:34.052 farads 0.0004100000 NA NA
 NA NA
2018-02-16 16:46:09.300 reactance 0.0004300000 2018-02-16 16:51:22.525 reactance 0.0005900000
2018-02-16 16:47:10.987 reactance 0.0002800000 2018-02-16 16:52:01.997 reactance 0.0003900000
2018-02-16 16:47:51.611 reactance 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:47:51.612 reactance 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:48:34.077 reactance 0.0006600000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:49:17.015 reactance 0.0003300000 2018-02-16 16:54:03.276 reactance 0.0005300000
2018-02-16 16:49:59.075 reactance 0.0000700000 2018-02-16 16:54:44.223 reactance 0.0003800000
2018-02-16 16:50:40.486 reactance 0.0002400000 2018-02-16 16:55:24.769 reactance 0.0003200000
2018-02-16 16:51:22.525 reactance 0.0005900000 2018-02-16 16:56:10.028 reactance 0.0002700000
2018-02-16 16:52:01.997 reactance 0.0003900000 2018-02-16 16:56:57.624 reactance 0.0000900000
2018-02-16 16:52:43.612 reactance 0.0005200000 2018-02-16 16:57:37.387 reactance 0.0003000000
2018-02-16 16:53:23.550 reactance 0.0003900000 2018-02-16 16:58:16.929 reactance 0.0005800000
2018-02-16 16:54:03.276 reactance 0.0005300000 2018-02-16 16:58:56.961 reactance 0.0003000000

Actually the match should be like this where lines that require match are the first four columns that come from file1 and the matched values are the last four columns that come from file2

2018-02-16 16:45:29.557 farads 0.0004300000 2018-02-16 16:50:40.486 reactance 0.0002400000
2018-02-16 16:46:09.300 farads 0.0004300000 2018-02-16 16:51:22.525 reactance 0.0005900000
2018-02-16 16:47:10.987 farads 0.0002800000 2018-02-16 16:52:01.997 reactance 0.0003900000
2018-02-16 16:47:51.611 farads 0.0006500000 2018-02-16 16:52:43.612 reactance 0.0005200000
2018-02-16 16:47:51.612 farads 0.0006500000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:48:34.077 farads 0.0006600000 2018-02-16 16:53:23.550 reactance 0.0003900000
2018-02-16 16:49:17.015 farads 0.0003300000 2018-02-16 16:54:03.276 reactance 0.0005300000
2018-02-16 16:49:59.075 farads 0.0000700000 2018-02-16 16:54:44.223 reactance 0.0003800000
2018-02-16 16:50:40.486 farads 0.0002400000 2018-02-16 16:55:24.769 reactance 0.0003200000
2018-02-16 16:51:22.525 farads 0.0005900000 2018-02-16 16:56:10.028 reactance 0.0002700000
2018-02-16 16:52:01.997 farads 0.0003900000 2018-02-16 16:56:57.624 reactance 0.0000900000
2018-02-16 16:52:43.612 farads 0.0005200000 2018-02-16 16:57:37.387 reactance 0.0003000000
2018-02-16 16:53:23.550 farads 0.0003900000 2018-02-16 16:58:16.929 reactance 0.0005800000
2018-02-16 16:54:03.276 farads 0.0005300000 2018-02-16 16:58:56.961 reactance 0.0003000000

For your first problem, try - based on the assumption that there's only few time stamps with duplicate seconds and norrmally large gaps in between - this simpler approach, which eliminates the need for a system call to date by adding the epoch time to every line upfront:

paste <(date +"%s" -f<(cut -d" " -f1,2 data.txt)) data.txt | awk '
$1 in LN        {$1++
                }
                {TM[NR] = $1
                 sub ($1 ".", _)
                 LN[TM[NR]] = $0
                }
END             {for (n=1; n<=NR; n++)  {TMP = TM[n] + 300
                                         DT  = 0
                                         for (SEC=0; SEC<120; SEC++)    {if ((TMP + SEC) in LN) DT = +SEC
                                                                         if ((TMP - SEC) in LN) DT = -SEC
                                                                         if (DT) break
                                                                        }
                                         OUT = LN[TMP+DT]
                                         sub  (/farads./, _, OUT)
                                         $0 = LN[TM[n]] OFS (OUT?OUT:"NA NA")
                                         print
                                        }
                }
'

For your other problem, try

paste <(date +"%s" -f<(cut -d" " -f1,2 file2)) file2 > TMP2
paste <(date +"%s" -f<(cut -d" " -f1,2 file1)) file1 > TMP1
awk '

FNR == NR       {if ($1 in LN)  $1++
                 TM[NR] = $1
                 sub ($1 ".", _)
                 LN[TM[NR]] = $0
                 next
                }

                {TMP = $1 + 300
                 DT  = 0
                 for (SEC=0; SEC<120; SEC++)    {if ((TMP + SEC) in LN) DT = +SEC
                                                 if ((TMP - SEC) in LN) DT = -SEC
                                                 if (DT) break
                                                }
                 OUT = LN[TMP+DT]
                 sub ($1 ".", _)
                 print $0  OFS (OUT?OUT:"NA NA")
                }
' TMP2 TMP1

This is matching the two files well. I am getting a warning that says
assignment used in conditional context

awk: awkscript5:30: (FILENAME=file2.txt FNR=175) warning: assignment used in conditional context

Oops yes, lint has picked up that this line:

         if( best = 9999999) deststart = cur

Is assigning to best (not comparing), I'd like to propose this solution:

#!/usr/bin/awk -f
FNR==1 {file++}
{
  day=$1
  gsub(/-/, " ", day)
  split($2, t, ".")
  gsub(/:/, " ", t[1])
  x=mktime(day " " t[1]) + t[2] / 1000
  if(file==1) srctime[FNR]=x
  else desttime[FNR]=x
  records[file, FNR]=$0
}

END {
   offset=5*60
   max=2*60
   deststart=0
   for(rec = 1; rec in srctime; rec++) {
       target = srctime[rec] + offset
       offsetmin = target - max
       offsetmax = target + max
       best = 9999999
       found = 0
       cur=deststart+1
       while(cur in desttime && desttime[cur] < offsetmax) {
           if (desttime[cur] < target && desttime[cur] > offsetmin &&
               best > target - desttime[cur]) {
                  if( best == 9999999) deststart = cur
                  best= target - desttime[cur]
                  found=cur
           }
           if (desttime[cur] >= target) {
              if(best > desttime[cur] - target) {
                  best=desttime[cur] - target
                  found=cur
               }
               break
           }
           cur++
        }

        if (found)
           print records[1, rec] " " records[2, found]
        else
           print records[1, rec] " NA NA"
    }
}

Which ensures that srctime is iterated in row number order and compares best with 9999999 instead of assigning.

1 Like