使用awk获取两个日期的时间差

使用awk获取两个日期的时间差

我有一个大小为 7GB 的文件。现在有两个日期时间,我想使用 awk 来获取这两个日期时间之间的时间差。

下面是我的文件的样子:

A          B      C      D         E
18/06/28 09:19:07 295  141536 18-06-28 09:17:47
18/06/28 09:20:07 268  1160   18-06-28 09:18:58
18/06/28 09:21:07 317  1454   18-06-28 09:19:47
18/06/28 09:22:07 275  1491   18-06-28 09:20:59
18/06/28 09:23:07 320  1870   18-06-28 09:21:07
18/06/28 09:24:07 310  1869   18-06-28 09:22:30
18/06/28 09:25:07 150   693   18-06-28 09:23:28
18/06/28 09:26:07 414  2227   18-06-28 09:24:34 

我想要 (AB) - (E) 之间的差异。

我试过这个:

cat filename | awk -F " " '{print date -d ($1$2)-($5)}' 

输出应该是两个日期时间之间的时间差。就像第一行的差异将是 1 分 20 秒

答案1

使用 GNU awk:

gawk '
  function dt2epoch(date, time,      timestr) {
    timestr = "20" substr(date,1,2) " " substr(date,4,2) " " substr(date,7,2) \
               " " substr(time,1,2) " " substr(time,4,2) " " substr(time,7,2)
    return mktime(timestr)
  }
  function epoch2hms(t) {
    return strftime("%H:%M:%S", t, 1)
  }
  function abs(n) {return n<0 ? -1*n : n}
  NR == 1 {next}
  { print epoch2hms(abs(dt2epoch($5,$6) - dt2epoch($1,$2))) }
' file

输出

00:01:20
00:01:09
00:01:20
00:01:08
00:02:00
00:01:37
00:01:39
00:01:33

对于perl,我会使用约会时间生态系统:

perl -MDateTime::Format::Strptime -lane '
    BEGIN {$f = DateTime::Format::Strptime->new(pattern => "%y-%m-%d %H:%M:%S")}
    next if $. == 1;
    $F[0] =~ s{/}{-}g;
    $t1 = $f->parse_datetime("$F[0] $F[1]");
    $t2 = $f->parse_datetime("$F[4] $F[5]");
    $d = $t1->subtract_datetime($t2);
    printf "%02d:%02d:%02d\n", $d->hours, $d->minutes, $d->seconds;
' file

更快的 perl 版本,不需要任何非核心模块

perl -MTime::Piece -lane '
    next if $. == 1;
    $t1 = Time::Piece->strptime("$F[0] $F[1]", "%y/%m/%d %H:%M:%S");
    $t2 = Time::Piece->strptime("$F[4] $F[5]", "%y-%m-%d %H:%M:%S");
    $diff = gmtime(abs($t1->epoch - $t2->epoch));
    print $diff->hms;
' file

或者,替代输出:

$ perl -MTime::Piece -lane '
    next if $. == 1;
    $t1 = Time::Piece->strptime("$F[0] $F[1]", "%y/%m/%d %H:%M:%S");
    $t2 = Time::Piece->strptime("$F[4] $F[5]", "%y-%m-%d %H:%M:%S");
    print abs($t1 - $t2)->pretty;
' file
1 minutes, 20 seconds
1 minutes, 9 seconds
1 minutes, 20 seconds
1 minutes, 8 seconds
2 minutes, 0 seconds
1 minutes, 37 seconds
1 minutes, 39 seconds
1 minutes, 33 seconds

答案2

使用bashawk组合:

$ awk 'NR>1 {print $1,$2,$5,$6}' input  | while read d1 t1 d2 t2; do
  i1=$(date -u -d "20$d1 $t1" +%s)
  i2=$(date -u -d "20$d1 $t2" +%s)
  date -d @"$((i1-i2))" +%M:%S; 
done
01:20
01:09
01:20
01:08
02:00
01:37
01:39
01:33

答案3

基准测试:我多次重复了你的样本数据

$ wc -l file
131073 file

现在一些时间安排:

$ time  awk 'NR>1 {print $1,$2,$5,$6}' file |
while read d1 t1 d2 t2; do
  i1=$(date -u -d "20$d1 $t1" +%s)
  i2=$(date -u -d "20$d1 $t2" +%s)
  date -d @"$((i1-i2))" +%M:%S
done > /dev/null

real    8m55.533s
user    5m46.956s
sys     1m33.726s
$ time perl -MDateTime::Format::Strptime -lane '
    BEGIN {$f = DateTime::Format::Strptime->new(pattern => "%y-%m-%d %H:%M:%S")}
    next if $. == 1;
    $F[0] =~ s{/}{-}g;
    $t1 = $f->parse_datetime("$F[0] $F[1]");
    $t2 = $f->parse_datetime("$F[4] $F[5]");
    $d = $t1->subtract_datetime($t2);printf "%02d:%02d:%02d\n", $d->hours, $d->minutes, $d->seconds;
' file > /dev/null

real    0m37.684s
user    0m33.168s
sys     0m0.058s
$ time gawk '
  function dt2epoch(date, time,      timestr) {
    timestr = "20" substr(date,1,2) " " substr(date,4,2) " " substr(date,7,2) \
               " " substr(time,1,2) " " substr(time,4,2) " " substr(time,7,2)
    return mktime(timestr)
  }
  function epoch2hms(t) {
    return strftime("%H:%M:%S", t, 1)
  }
  function abs(n) {return n<0 ? -1*n : n}
  NR == 1 {next}
  { print epoch2hms(abs(dt2epoch($5,$6) - dt2epoch($1,$2))) }
' file > /dev/null

real    0m1.074s
user    0m0.610s
sys     0m0.366s

具有内置时间函数的 GNU awk 显然是赢家,即使进行所有字符串操作也是如此。


更新:新的 perl 实现。仍然比 gawk 慢,但比使用功能丰富但重量级的 DateTime 模块的版本领先很多:

$ time perl -MTime::Piece -lane '
    next if $. == 1;
    $t1 = Time::Piece->strptime("$F[0] $F[1]", "%y/%m/%d %H:%M:%S");
    $t2 = Time::Piece->strptime("$F[4] $F[5]", "%y-%m-%d %H:%M:%S");
    $diff = gmtime(abs($t1->epoch - $t2->epoch));
    print $diff->hms;           
' file > /dev/null

real    0m2.631s
user    0m2.231s
sys     0m0.170s

相关内容