gpt4 book ai didi

linux - 如何在 Linux 中加速 awk 命令?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:25:58 26 4
gpt4 key购买 nike

我在 linux 中使用 awk 命令将 utc 转换为本地时间,但文件大小很大 (>30 gb) 并且需要一个多小时。

这是我的代码:

awk -F"," '{cmd="date -d \"$(date -d \""$1"\")-4hours\" \"+%Y%m%d_%H\"";cmd | getline datum; close(cmd); print $0 ","datum""}' data.txt

我怎样才能加快这个命令的速度,或者有什么简单的方法可以在 linux 中进行这种转换?

Here is the sample of input:

utc,id
2018-03-31 16:00:49.425,4485
2018-04-1 17:01:19.425,30019
2018-05-31 18:01:49.425,15427
2018-08-20 19:01:55.425,17579
2018-09-2 20:02:31.425,23716
2018-10-15 21:03:34.425,24772

expected output:

utc,id,localtime
2018-03-31 16:00:49.425,4485,20180331_12
2018-04-1 17:01:19.425,30019,20180401_13
2018-05-31 18:01:49.425,15427,20180531_14
2018-08-20 19:01:55.425,17579,20180820_15
2018-09-2 20:02:31.425,23716,20180902_16
2018-10-15 21:03:34.425,24772,20181015_17

最佳答案

你原来的解决方案之所以慢是因为系统调用了date。您使用 awk 处理的每条记录/行都会调用外部命令来执行日期转换。这样的外部调用需要加载到内存中执行,其输出需要由 awk 处理。如果我们可以在 awk 中进行实际的日期转换,我们可以加快速度。


一般评论:当您将日期和时间从 UTC 转换为您本地的时区时,您必须考虑到 1 月 1 日与 8 月 1 日处于不同的时区。这是由于日光 -节约时间。下面的算法没有提供解决方案,因为 OP 只是要求移动 4 小时,或者移动到他当前的时区。 (备注:gawk 4.1.2 或更新版本的解决方案将考虑 DST)


下面我介绍了几种解决方案,您可以根据使用的 awk 使用这些解决方案:

Gnu awkgawk 的各种扩展之一是时间函数。此问题的两个有用的时间函数是 mktimestrftime:

  • mktime(datespec,[utc-flag]): This converts a date specification string, datespec, of the form YYYY MM DD hh mm ss into a Unix epoch time, i.e. the total seconds since 1970 01 01 UTC. Since gawk-4.2.1 you can use the utc-flag to indicate datespec is in UTC or not. Prior to gawk-4.2.1 it assumed the local time-zone.

  • strftime(format,timestamp,[utc-flag]): This converts an epoch-time timestamp into a formatted string (same formatting as the date command). You can use the utc-flag the indicate that the returned time should be in UTC or in the local time-zone.

More info in the GNU awk manual

我们想将字段 1 从 UTC 转换为本地时区。由于我们不知道字段 1 的格式,我们假设存在一个函数 convert_time(str)str 格式转换为 YYYY MM DD hh mm ss 可以被mktime接受:

gawk 4.1.2 或更新版本:

$ awk 'BEGIN{FS=OFS=","}
{ # convert $1 into YYYY MM DD hh mm ss
datestring=convert_time($1)
# convert datestring (as UTC) into epoch
datum=mktime(datestring,1)
# convert epoch into string (local TZ)
datum=strftime("\042%Y%m%d_%H\042",datum)
# print and append
print $0,datum
}' data.txt

在 gawk 4.1.2 之前:这里我们不能使用 utc-flag,因此我们强制 awk 在 UTC 中工作:

$ TZ=UTC awk 'BEGIN{FS=OFS=","}
{ # convert $1 into YYYY MM DD hh mm ss
datestring=convert_time($1)
# convert datestring (as UTC) into epoch
datum=mktime(datestring)
# perform TZ correction
datum-=4*3600;
# convert epoch into string (local TZ)
datum=strftime("\042%Y%m%d_%H\042",datum)
# print and append
print $0,datum
}' data.txt

POSIX awk:如果您没有 GNU awk,但没有任何其他 awk,则不能使用这些时间函数,因为它们是特定于 GNU awk 的。但是可以实现它们:

awk '

# Algorithm from "Astronomical Algorithms" By J.Meeus
function mktime_posix(datestring, a,t) {
split(datestring,a," ")
if (a[1] < 1970) return -1
if (a[2] <= 2) { a[1]--; a[2]+=12 }
t=int(a[1]/100); t=2-t+int(t/4)
t=int(365.25*a[1]) + int(30.6001*(a[2]+1)) + a[3] + t - 719593
return t*86400 + a[4]*3600 + a[5]*60 + a[6]
}

function strftime_posix(epoch, JD,yyyy,mm,dd,HH,MM,SS,A,B,C,D,E ) {
if (epoch < 0 ) return "0000 00 00 00 00 00.000000"
JD=epoch; SS=JD%60; JD-=SS; JD/=60; MM=JD%60;
JD-=MM; JD/=60; HH=JD%24; JD-=HH; JD/=24;
JD+=2440588
A=int((JD - 1867216.25)/(36524.25))
A=JD+1+A-int(A/4)
B=A+1524; C=int((B-122.1)/365.25); D=int(365.25*C); E=int((B-D)/30.6001)
dd=B-D-int(30.6001*E)
mm = E < 14 ? E-1 : E - 13
yyyy=mm>2?C-4716:C-4715
return sprintf("\042%0.4d-%0.2d-%0.2d %0.2d:%0.2d:%06.3f",yyyy,mm,dd,HH,MM,SS)
}

{ # convert $1 into YYYY MM DD hh mm ss
datestring=convert_time($1)
# convert datestring (as UTC) into epoch
datum=mktime_posix(datestring)
# perform TZ correction
datum-=4*3600;
# convert epoch into string (local TZ)
datum=strftime_posix(datum)
# print and append
print $0,datum
}' data.txt

关于linux - 如何在 Linux 中加速 awk 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56235818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com