Corresponde ao timestamp mais próximo em milissegundos

0

Eu tenho um grande arquivo data.txt como abaixo; para cada timestamp, preciso encontrar a correspondência de tempo mais próxima 5 minutos depois e o valor na coluna 4. Agora, imprima os dois em duas novas colunas.

por exemplo, para "2018-02-16 16: 45: 29,557 farads 0,0004300000" Eu preciso encontrar o melhor tempo disponível 5 minutos depois (ou seja, aproximadamente) 16: 50: 40.486 farads 0.0002400000.

Por favor, note que não há correspondência exata de hora aqui, e é por isso que apenas a correspondência mais próxima é necessária. Além disso, o código deve funcionar perfeitamente quando a data for alterada de 2018-02-16 para 2018-02-17.

saída final deve ser

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

Como posso fazer isso acontecer?

   col1       col2       col3      col4
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
    
por kalamata_Olive 05.03.2018 / 20:52

2 respostas

0

Aqui está uma opção usando awk e data GNU; ele usa date para fazer o "trabalho pesado" de calcular os registros de data e hora em segundos fracionários dos campos de data e hora; O awk então armazena cada registro em uma matriz indexada por esse timestamp. Um efeito colateral disso é que os registros duplicados são "apagados" - somente o resultado é retornado para a entrada 16:45:29.557 . Depois de computar todos os timestamps-in-seconds, percorremos os registros procurando a correspondência mais próxima de "+5 minutos" de cada entrada. Se a diferença de timestamp da entrada mais próxima for menor que 2 minutos (da meta de 5 minutos), então imprimimos a data e a hora correspondentes e a leitura da farad (simplesmente substituindo a string "farads" pela string vazia); caso contrário, imprimiremos os campos "NA", como por seu comentário . Isso é um desvio de sua especificação, na suposição de que uma entrada correspondente para um determinado registro poderia ser "amanhã".

{
  day=$1
  split($2, t, ".")
  x=day " " t[1]
  ("date --date=\""x"\" +%s") | getline seconds
  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]
    }
  }
}

Saída da entrada de amostra (não classificada):

 2018-02-16      16:51:22.525    farads  0.0005900000 NA NA
 2018-02-16      16:45:29.558    farads  0.0004300000  2018-02-16      16:50:40.486      0.0002400000
 2018-02-16      16:45:29.557    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:50:40.486    farads  0.0002400000 NA NA
 2018-02-16      16:48:34.077    farads  0.0006600000  2018-02-16      16:52:01.997      0.0003900000
 2018-02-16      16:47:51.612    farads  0.0006500000  2018-02-16      16:52:01.997      0.0003900000
 2018-02-16      16:47:51.611    farads  0.0006500000  2018-02-16      16:52:01.997      0.0003900000
 2018-02-16      16:49:59.075    farads  0.0000700000 NA NA
 2018-02-16      16:49:17.015    farads  0.0003300000 NA NA
 2018-02-16      16:52:01.997    farads  0.0003900000 NA NA
 2018-02-16      16:47:10.987    farads  0.0002800000  2018-02-16      16:52:01.997      0.0003900000

... e canalizado para |sort :

 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:01.997      0.0003900000
 2018-02-16      16:47:51.612    farads  0.0006500000  2018-02-16      16:52:01.997      0.0003900000
 2018-02-16      16:48:34.077    farads  0.0006600000  2018-02-16      16:52:01.997      0.0003900000
 2018-02-16      16:49:17.015    farads  0.0003300000 NA NA
 2018-02-16      16:49:59.075    farads  0.0000700000 NA NA
 2018-02-16      16:50:40.486    farads  0.0002400000 NA NA
 2018-02-16      16:51:22.525    farads  0.0005900000 NA NA
 2018-02-16      16:52:01.997    farads  0.0003900000 NA NA

A saída do script, executada na entrada expandida, é:

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

Observação: quando eu postava isso originalmente, o código tinha um bug que fazia com que os tempos que faltavam cinco minutos antes da hora não fossem correspondidos com os horários posteriores correspondentes. Como não tive tempo para consertar o bug, apaguei o post, mas agora esse bug deve ser corrigido e não recuperei a resposta. No entanto, desde que você forneceu apenas uma única linha de saída esperada, , por favor, tome cuidado para testar este código em relação a um conjunto mais amplo de entradas de amostra.

Você disse que não está tão preocupado com o tempo de CPU, mas com minha tentativa inicial que simplesmente bruta forçado, comparando cada linha para todas as outras linhas levou 30s para executar em um arquivo de linha 1800, então eu otimizei com o %mins hash, digitado o tempo para baixo para o minuto, de modo que apenas minutos que são +4, +5 e +6 minutos de cada timestamp são levados em consideração. Esta versão leva ~ 4s para rodar em um arquivo de entrada de ~ 4000 linhas.

use warnings;
use strict;
use DateTime;
use DateTime::Format::Strptime;

my $strp = DateTime::Format::Strptime->new( on_error=>'croak',
    pattern => '%Y-%m-%d %H:%M:%S.%3N' );
my (@recs, %mins);
while (<>) {
    m{\A\s* ([-\d]+) \s+ ([\d:.]+) \s+farads\s+ ((?:\d+\.)?\d+) \s*\z}ixms
        or die "failed to parse line: $_";
    my $dt  = $strp->parse_datetime("$1 $2");
    my $min = int($dt->epoch/60);
    my $rec = { min=>$min, farads=>$3, dt=>$dt, seek=>$dt->clone->add(minutes=>5) };
    push @{ $mins{ $min } }, $rec;
    push @recs, $rec;
}

for my $cur (@recs) {
    my @candidates = (
        @{ $mins{$cur->{min}+4} // [] },
        @{ $mins{$cur->{min}+5} // [] },
        @{ $mins{$cur->{min}+6} // [] }  );
    my ($min_diff_ms,$min_other);
    for my $other (@candidates) {
        my $diff_ms = abs($cur->{seek}->subtract_datetime_absolute($other->{dt})
            ->in_units('nanoseconds'))/1e6;
        if (!defined $min_diff_ms || $diff_ms<$min_diff_ms) {
            $min_diff_ms = $diff_ms;
            $min_other = $other;
        }
    }
    print $cur->{dt}->strftime('%Y-%m-%d %H:%M:%S.%3N')," farads ",
        $cur->{farads}, defined($min_other) ? ( " ",
            $min_other->{dt}->strftime('%Y-%m-%d %H:%M:%S.%3N')," ",
            $min_other->{farads} ) : '', "\n";
}

Sua entrada de amostra original:

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

Saída para essa entrada:

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.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:01.997 0.0003900000
2018-02-16 16:47:51.612 farads 0.0006500000 2018-02-16 16:52:01.997 0.0003900000
2018-02-16 16:48:34.077 farads 0.0006600000 2018-02-16 16:52:01.997 0.0003900000
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

Aqui está uma versão ainda mais rápida, mas menos precisa, ignora milissegundos e usa o módulo principal Time::Piece em vez do um pouco pesado DateTime (embora eu ainda possa recomendar o último). Comparado com o acima, leva uma fração de segundo para ser executado. Você pode ver a diferença na precisão, mas você disse que ± 1 minuto é aceitável.

use warnings;
use strict;
use Time::Piece;

my (@recs, %mins);
while (<>) {
    m{\A\s* ([-\d]+) \s+ ([\d:]+)(\.\d+) \s+farads\s+ ((?:\d+\.)?\d+) \s*\z}ixms
        or die "failed to parse line: $_";
    my $dt  = Time::Piece->strptime("$1 $2", '%Y-%m-%d %H:%M:%S');
    my $min = int($dt->epoch/60);
    my $rec = { min=>$min, farads=>$4, timestr=>"$1 $2$3", epoch=>$dt->epoch };
    push @{ $mins{ $min } }, $rec;
    push @recs, $rec;
}

for my $cur (@recs) {
    my @candidates = (
        @{ $mins{$cur->{min}+4} // [] },
        @{ $mins{$cur->{min}+5} // [] },
        @{ $mins{$cur->{min}+6} // [] }  );
    my ($min_diff_s,$min_other);
    for my $other (@candidates) {
        my $diff_s = abs( $other->{epoch} - $cur->{epoch} );
        if (!defined $min_diff_s || $diff_s<$min_diff_s) {
            $min_diff_s = $diff_s;
            $min_other = $other;
        }
    }
    print $cur->{timestr}," farads ",$cur->{farads},
        defined($min_other)
            ? ( " ", $min_other->{timestr}," ",$min_other->{farads} )
            : '', "\n";
}

Saída:

2018-02-16 16:45:29.557 farads 0.0004300000 2018-02-16 16:49:17.015 0.0003300000
2018-02-16 16:45:29.557 farads 0.0004300000 2018-02-16 16:49:17.015 0.0003300000
2018-02-16 16:45:29.558 farads 0.0004300000 2018-02-16 16:49:17.015 0.0003300000
2018-02-16 16:46:09.300 farads 0.0004300000 2018-02-16 16:50:40.486 0.0002400000
2018-02-16 16:47:10.987 farads 0.0002800000 2018-02-16 16:51:22.525 0.0005900000
2018-02-16 16:47:51.611 farads 0.0006500000 2018-02-16 16:51:22.525 0.0005900000
2018-02-16 16:47:51.612 farads 0.0006500000 2018-02-16 16:51:22.525 0.0005900000
2018-02-16 16:48:34.077 farads 0.0006600000 2018-02-16 16:52:01.997 0.0003900000
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

(note que normalmente não teria fornecido uma solução completa para uma pergunta sem código, mas essa foi uma questão interessante para mim.)

    
por 06.03.2018 / 00:03