查找文本文件中的所有时间戳并使用 SED 或 AWK linux 将此时间戳转换为 (UTC-5)
Find all timestamp in textfile and convert this timestamp in (UTC-5) with SED or AWK linux
我在 linux ubuntu 中遇到以下问题,
我需要在我的文本文件中找到所有字符串时间戳(UTC),并使用 bash 脚本
转换为时区 UTC -5 的时间戳
我需要使用 sed 或 awk 的脚本
带有 UTC 时间戳的原始文本文件是:
罚款:2021/10/24 14:02:04,周六:
晴:2021/10/24 14:02:04,周六:
晴:2021/10/24 14:02:04,周六:
信息:2021/10/24 14:02:04,GRDi
罚款:2021/10/24 14:02:04,GRDi
更精细:2021/10/24 14:02:05,GRD
罚款:2021/10/24 14:02:05,x,DB
罚款:2021/10/24 14:10:44,服务
配置:2021/10/24 14:10:44,x,
更好:2021/10/24 14:10:44,SAT
晴:2021/10/24 14:10:44,周六:
我需要转换时间戳 (UTC-5),输出文件是这样的:
精细:2021/10/24 09:02:04,周六:
晴:2021/10/24 09:02:04,周六:
晴:2021/10/24 09:02:04,周六:
信息:2021/10/24 09:02:04,GRDi
罚款:2021/10/24 09:02:04,GRDi
更精细:2021/10/24 09:02:05,GRD
罚款:2021/10/24 09:02:05,x,DB
罚款:2021/10/24 09:10:44,服务
配置:2021/10/24 09:10:44,x,
更好:2021/10/24 09:10:44,SAT
晴:2021/10/24 09:10:44,周六:
感谢您的帮助...
这是实现此目的的一种方法...将 脚本 保存为例如minus5h.awk,
{
one = gensub(/^([^ ]+ ).*/, "\1", 1, [=10=]). # store the beginning of line
rest = gensub(/[^,]+,(.*)$/, "\1", 1, [=10=]). # store the rest of the line after the time-spec
split(, a, "/") # split the date into chunks
split(gensub(/([^,]+).*/,"\1",1,), b, ":") # split the time into chunks
c = mktime(a[1] " " a[2] " " a[3] " " b[1] " " b[2] " " b[3]) - 18000 # convert the date into seconds and subtract 5 hours
printf "%s%s,%s\n", one, strftime("%Y/%m/%d %H:%M:%S", c), rest # print everything with the new timestamp
}
然后 运行 就像这样:
awk -f minus5h.awk logfile
FINE: 2021/10/24 09:02:04,SAT:
FINE: 2021/10/24 09:02:04,SAT:
FINE: 2021/10/24 09:02:04,SAT:
INFO: 2021/10/24 09:02:04,GRDi
FINE: 2021/10/24 09:02:04,GRDi
FINER: 2021/10/24 09:02:05,GRD
FINE: 2021/10/24 09:02:05,x,DB
FINE: 2021/10/24 09:10:44,SERV
CONFIG: 2021/10/24 09:10:44,x,
FINER: 2021/10/24 09:10:44,SAT
FINE: 2021/10/24 09:10:44,SAT:
我在 linux ubuntu 中遇到以下问题, 我需要在我的文本文件中找到所有字符串时间戳(UTC),并使用 bash 脚本
转换为时区 UTC -5 的时间戳我需要使用 sed 或 awk 的脚本
带有 UTC 时间戳的原始文本文件是:
罚款:2021/10/24 14:02:04,周六:
晴:2021/10/24 14:02:04,周六:
晴:2021/10/24 14:02:04,周六:
信息:2021/10/24 14:02:04,GRDi
罚款:2021/10/24 14:02:04,GRDi
更精细:2021/10/24 14:02:05,GRD
罚款:2021/10/24 14:02:05,x,DB
罚款:2021/10/24 14:10:44,服务
配置:2021/10/24 14:10:44,x,
更好:2021/10/24 14:10:44,SAT
晴:2021/10/24 14:10:44,周六:
我需要转换时间戳 (UTC-5),输出文件是这样的:
精细:2021/10/24 09:02:04,周六:
晴:2021/10/24 09:02:04,周六:
晴:2021/10/24 09:02:04,周六:
信息:2021/10/24 09:02:04,GRDi
罚款:2021/10/24 09:02:04,GRDi
更精细:2021/10/24 09:02:05,GRD
罚款:2021/10/24 09:02:05,x,DB
罚款:2021/10/24 09:10:44,服务
配置:2021/10/24 09:10:44,x,
更好:2021/10/24 09:10:44,SAT
晴:2021/10/24 09:10:44,周六:
感谢您的帮助...
这是实现此目的的一种方法...将 脚本 保存为例如minus5h.awk,
{
one = gensub(/^([^ ]+ ).*/, "\1", 1, [=10=]). # store the beginning of line
rest = gensub(/[^,]+,(.*)$/, "\1", 1, [=10=]). # store the rest of the line after the time-spec
split(, a, "/") # split the date into chunks
split(gensub(/([^,]+).*/,"\1",1,), b, ":") # split the time into chunks
c = mktime(a[1] " " a[2] " " a[3] " " b[1] " " b[2] " " b[3]) - 18000 # convert the date into seconds and subtract 5 hours
printf "%s%s,%s\n", one, strftime("%Y/%m/%d %H:%M:%S", c), rest # print everything with the new timestamp
}
然后 运行 就像这样:
awk -f minus5h.awk logfile
FINE: 2021/10/24 09:02:04,SAT:
FINE: 2021/10/24 09:02:04,SAT:
FINE: 2021/10/24 09:02:04,SAT:
INFO: 2021/10/24 09:02:04,GRDi
FINE: 2021/10/24 09:02:04,GRDi
FINER: 2021/10/24 09:02:05,GRD
FINE: 2021/10/24 09:02:05,x,DB
FINE: 2021/10/24 09:10:44,SERV
CONFIG: 2021/10/24 09:10:44,x,
FINER: 2021/10/24 09:10:44,SAT
FINE: 2021/10/24 09:10:44,SAT: