- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 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
的各种扩展之一是时间函数。此问题的两个有用的时间函数是 mktime
和 strftime
:
mktime(datespec,[utc-flag])
: This converts a date specification string,datespec
, of the formYYYY 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 theutc-flag
to indicatedatespec
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-timetimestamp
into a formatted string (same formatting as thedate
command). You can use theutc-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/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
Linux 管道可以缓冲多少数据?这是可配置的吗? 如果管道的两端在同一个进程中,但线程不同,这会有什么不同吗? 请注意:这个“同一个进程,两个线程”的问题是理论上的边栏,真正的问题是关于缓冲的。 最
我找到了here [最后一页] 一种有趣的通过 Linux 启动 Linux 的方法。不幸的是,它只是被提及,我在网上找不到任何有用的链接。那么有人听说过一种避免引导加载程序而使用 Linux 的方法
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我试图了解 ld-linux.so 如何在 Linux 上解析对版本化符号的引用。我有以下文件: 测试.c: void f(); int main() { f(); } a.c 和 b.c:
与 RetroPie 的工作原理类似,我可以使用 Linux 应用程序作为我的桌面环境吗?我实际上并不需要像实际桌面和安装应用程序这样的东西。我只需要一种干净简单的方法来在 RaspberryPi 上
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
有什么方法可以覆盖现有的源代码,我应该用 PyQt、PyGTK、Java 等从头开始构建吗? 最佳答案 如果您指的是软件本身而不是它所连接的存储库,那么自定义应用程序的方法就是 fork 项目。据我所
我的情况是:我在一个磁盘上安装了两个 linux。我将第一个安装在/dev/sda1 中,然后在/dev/sda2 中安装第二个然后我运行第一个系统,我写了一个脚本来在第一个系统运行时更新它。
我在 i2c-0 总线上使用地址为 0x3f 的系统监视器设备。该设备在设备树中配置有 pmbus 驱动程序。 问题是,加载 linux 内核时,这个“Sysmon”设备没有供电。因此,当我在总线 0
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我正试图在 linux 模块中分配一大块内存,而 kalloc 做不到。 我知道唯一的方法是使用 alloc_bootmem(unsigned long size) 但我只能从 linux 内核而不是
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我有 .sh 文件来运行应用程序。在该文件中,我想动态设置服务器名称,而不是每次都配置。 我尝试了以下方法,它在 CentOS 中运行良好。 nohup /voip/java/jdk1.8.0_71/
我是在 Linux 上开发嵌入式 C++ 程序的新手。我有我的 Debian 操作系统,我在其中开发和编译了我的 C++ 项目(一个简单的控制台进程)。 我想将我的应用程序放到另一个 Debian 操
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我使用4.19.78版本的稳定内核,我想找到带有企鹅二进制数据的C数组。系统启动时显示。我需要在哪里搜索该内容? 我在 include/linux/linux_logo.h 文件中只找到了一些 Log
我知道可以使用 gdb 的服务器模式远程调试代码,我知道可以调试针对另一种架构交叉编译的代码,但是是否可以更进一步,从远程调试 Linux 应用程序OS X 使用 gdbserver? 最佳答案 当然
是否有任何可能的方法来运行在另一个 Linux 上编译的二进制文件?我知道当然最简单的是在另一台机器上重建它,但假设我们唯一能得到的是一个二进制文件,那么这可能与否? (我知道这可能并不容易,但我只是
我是一名优秀的程序员,十分优秀!