我的LogFile:
created_at,entry_id,field1
2017-09-08 13:21:12 UTC,69,39.00
2017-09-08 14:20:03 UTC,70,42.00
2017-09-08 15:18:32 UTC,71,43.00
2017-09-08 16:16:59 UTC,72,44.00
2017-09-08 17:15:53 UTC,73,44.00我想重写LogFile,将TimeStamp从UTC转换为GMT +2,并将,替换为Tabs。
转换时间戳并用bash替换c的最佳方法是什么?
发布于 2017-09-12 21:07:56
快速而肮脏的:
perl -MTime::Piece -F, -ane '
if ($. > 1) {
$t = Time::Piece->strptime($F[0], "%Y-%m-%d %T UTC");
$F[0] = ($t + 7200)->strftime("%Y-%m-%d %T+02:00");
}
print join "\t", @F
' file如果我想更有力地做到这一点,并使用Olson时区来处理夏时制,我将使用非核心模块并这样做:
perl -MDateTime::Format::Strptime -F, -sane '
BEGIN {
$fmt = "%F %T %Z";
$p = DateTime::Format::Strptime->new(pattern => $fmt, time_zone => "UTC");
}
if ($. > 1) {
$t = $p->parse_datetime($F[0]);
$F[0] = $t->set_time_zone($dest_tz)->strftime($fmt);
}
print join "\t", @F
' -- -dest_tz="Europe/Paris" filecreated_at entry_id field1
2017-09-08 15:21:12 CEST 69 39.00
2017-09-08 16:20:03 CEST 70 42.00
2017-09-08 17:18:32 CEST 71 43.00
2017-09-08 18:16:59 CEST 72 44.00
2017-09-08 19:15:53 CEST 73 44.00调整您的目标时区以适应。
https://stackoverflow.com/questions/46184565
复制相似问题