awk matching script not working as expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk matching script not working as expected
# 1  
Old 03-21-2018
awk matching script not working as expected

This is my ubuntu version:

Code:
$ 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
Code:
 2018-02-16 16:45:29.557 farads 0.0004300000

I need to find the best time available 5 minutes later (i. e. approximately)
Code:
 16:50:40.486 0.0002400000

So the output line must be
Code:
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

Code:
 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:

Code:
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.

Code:
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:

Code:
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:

Code:
#!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

Code:
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

Last edited by delbroooks; 03-21-2018 at 11:41 PM..
# 2  
Old 03-21-2018
Code:
#!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]
       }
   }
}


Last edited by rdrtx1; 03-21-2018 at 06:17 PM..
# 3  
Old 03-21-2018
your proposed changes
Code:
if (!lmin++) lmin=0 and min=difference + 1

as well as Replacing
Code:
min=(0+"inf") WITH min="inf"

is printing only "NA NA" in the last two columns it's not matching anything
Code:
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.

Last edited by delbroooks; 03-21-2018 at 06:28 PM.. Reason: tried the suggested changes
# 4  
Old 03-21-2018
Quote:
Originally Posted by delbroooks
. . .
Code:
$ /bin/awk -V | head -n1
bash: /bin/awk: No such file or directory

Are you sure your script is starting at all?


Quote:
. . . find the best time available 5 minutes later (i. e. approximately)
What is "approximately 5 minutes later"? Within 5 min? Beyond 5 min? 10 sec / 30 sec / 100 sec within / beyond?

Quote:
. . . The expected output should look like this
Code:
 2018-02-16 16:42:53.926 farads 0.0000000000 2018-02-16 16:47:51.612 0.0006500000

Why the 612 ms, not the 611 ms entry?

Quote:
Code:
#!usr/bin/awk

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

Quote:
Code:
{
  day=$1
  split($2, t, ".")
.
.
.
  t[3]=seconds"."t[2]
  records[t[3]]=$0
}

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

Quote:
Code:
.
.
.
      min=(0 + "INF")              # whatever this means - it will yield min = 0
.
.
.
      if (difference < 0) difference=-difference
      if (difference < min) {
      min=difference
      matchfor[record]=records[others]
      }

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

Quote:
Code:
awk: awkscript3:29: (FILENAME=- FNR=183) warning: reference to 
uninitialized element `matchfor["1518802491.175"]'
.
.
.

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"

Last edited by RudiC; 03-21-2018 at 08:58 PM..
# 5  
Old 03-21-2018
@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.
# 6  
Old 03-22-2018
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.

Code:
#!/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"
    }
}


Last edited by Chubler_XL; 03-22-2018 at 12:22 AM..
This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 03-22-2018
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
Code:
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
Code:
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
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Why this script is not working as 'expected' when doing ssh with while read ... really confused?

Hi, I have a script below that is running ssh <host> <command> on some servers. Below is more or less the script. I have to modify it somehow to get rid of the 'confidential' hostnames check_log.bash #!/bin/bash # myPID=$$ parse_log () { sub="parse_log" host=${1} ... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. UNIX for Beginners Questions & Answers

Passing Arguments to shell script from file is not working as expected.

Hi All, I have below simple shell script in cloudera quick start vm cenos 6 which copy file from source to destination. # file_copy.sh source_dir = ${source_dir} target = ${target_dir} cp source_dir target and my parameter file is like below #parameter_file.txt source_dir =... (4 Replies)
Discussion started by: Narasimhasss
4 Replies

3. Shell Programming and Scripting

awk gsub not working as expected

Hi Experts, Need your kind help with gsub awk. Below is my pattern:"exec=1_host_cnt=100_dup=4_NameTag=targetSrv_500.csv","'20171122112948"," 100"," 1"," 1"," 4","400","","", " aac sample exec ""hostname=XXXXX commandline='timeout 10 openssl speed -multi 2 ; exit 0'"" ","-1","-1","1","... (6 Replies)
Discussion started by: pradyumnajpn10
6 Replies

4. Shell Programming and Scripting

awk command not working as expected

Following one line of awk code removes first 3 characters from each line but when I run the same code on another linux platform it doesn't work and only prints blank lines for each record. Can anyone please explain why this doesn't work? (31 Replies)
Discussion started by: later_troy
31 Replies

5. Shell Programming and Scripting

awk not working as expected in script

Dear all, I had script which used to work, but recently it is not working as expected. I have command line in my shell script to choose the following format from the output_elog and perform some task afterwards on As you see, I want all numbers in foramt following RED mark except for... (12 Replies)
Discussion started by: emily
12 Replies

6. Shell Programming and Scripting

Script not working as expected

Hi, I have prepared a script and trying to execute it but not getting expected output. Could you please help and advise what is going wrong. "If else" part in below script is not working basically. I am running it on HP-UX. for i in slpd puma sfmdb do echo "******\t$i\t*******" echo... (10 Replies)
Discussion started by: sv0081493
10 Replies

7. Shell Programming and Scripting

bash variable (set via awk+sed) not working as expected

Hi! Been working on a script and I've been having a problem. I've finally narrowed it down to this variable I'm setting: servername=$(awk -v FS=\/ '{ print $7 } blah.txt | sed 's\/./-/g' | awk -v FS=\- '{print $1}')" This will essentially pare down a line like this: ... (7 Replies)
Discussion started by: creativedynamo
7 Replies

8. Shell Programming and Scripting

Var substitution in awk - not working as expected

countA=`awk '/X/''{print substr($0,38,1)}' fName | wc -l` countB=`wc -l fName | awk '{print int($1)}'` echo > temp ratio=`awk -va=$countA -vc=$countB '{printf "%.4f", a/c}' temp` After running script for above I am getting an error as : awk: 0602-533 Cannot find or open file -vc=25. The... (3 Replies)
Discussion started by: videsh77
3 Replies

9. Shell Programming and Scripting

awk not working as expected with BIG files ...

I am facing some strange problem. I know, there is only one record in a file 'test.txt' which starts with 'X' I ensure that with following command, awk /^X/ test.txt | wc -l This gives me output = '1'. Now I take out this record out of the file, as follows : awk /^X/ test.txt >... (1 Reply)
Discussion started by: videsh77
1 Replies

10. Shell Programming and Scripting

which not working as expected

Hello. Consider the following magic words: # ls `which adduser` ls: /usr/sbin/adduser: No such file or directory # Hmmm... Then: # ls /usr/sbin/adduser /usr/sbin/adduser # Now what? Unforunately this little sniippet is used in my debian woody server's mysql pre install script.... (2 Replies)
Discussion started by: osee
2 Replies
Login or Register to Ask a Question