使用 AWK 使用 3 个字符的月份名称缩写格式化日期
Format date with a 3-char month name abbreviation using AWK
我有一个日期时间格式,如下例所示,我想用 AWK 将其转换为 dd-mm-yyyy hh:mm:ss
。我该怎么做?
当前格式:
3Jun2020 9:33:24; HG3456
7Jun2020 15:25:10; CH4747
10Jun2020 8:49:18; EU4821
12Jun2020 7:13:57; PP3478
期望的输出:
03-06-2020 09:33:24; HG3456
07-06-2020 15:25:10; CH4747
10-06-2020 08:49:18; EU4821
12-06-2020 07:13:57; PP3478
perl -MPOSIX -MDate::Parse -pe 's{^\S+\s+\S+(?=;)}{strftime("%d-%m-%Y %T", strptime($&))}e; s/^0//' file
我将按照以下方式使用 GNU AWK
完成此任务,令 file.txt
内容为
3Jun2020 9:33:24; HG3456
7Jun2020 15:25:10; CH4747
10Jun2020 8:49:18; EU4821
12Jun2020 7:13:57; PP3478
然后
awk '{sub(/Jan/,"-01-",);sub(/Feb/,"-02-",);sub(/Mar/,"-03-",);sub(/Apr/,"-04-",);sub(/May/,"-05-",);sub(/Jun/,"-06-",);sub(/Jul/,"-07-",);sub(/Aug/,"-08-",);sub(/Sep/,"-09-",);sub(/Oct/,"-10-",);sub(/Nov/,"-11-",);sub(/Dec/,"-12-",);print}' file.txt
输出
3-06-2020 9:33:24; HG3456
7-06-2020 15:25:10; CH4747
10-06-2020 8:49:18; EU4821
12-06-2020 7:13:57; PP3478
解释:用-01-
替换Jan
,用-02-
替换Feb
,用-03-
替换Mar
等等,然后print
。免责声明:如果您使用其他语言环境,代码可能需要调整。
(在 GNU Awk 5.0.1 中测试)
如果 sed
是一个选项,您可以 e
在替换中执行 date
命令。
$ sed "s/\([^;]*\)\(.*\)/\date -d '' '+%d-%m-%Y %T'/e" input_file
03-06-2020 09:33:24; HG3456
07-06-2020 15:25:10; CH4747
10-06-2020 08:49:18; EU4821
12-06-2020 07:13:57; PP3478
在每个 Unix 机器上的任何 shell 中使用任何 awk:
$ cat tst.awk
BEGIN { FS=OFS=";" }
{
split(,t,/[ :]/)
lgth = length(t[1])
dayNr = substr(t[1],1,lgth - 7)
mthAbbr = substr(t[1],lgth - 6,3)
mthNr = (index("JanFebMarAprMayJunJulAugSepOctNovDec",mthAbbr) + 2) / 3
yrNr = substr(t[1],lgth - 3)
= sprintf("%02d-%02d-%04d %02d:%02d:%02d", dayNr, mthNr, yrNr, t[2], t[3], t[4])
print
}
$ awk -f tst.awk file
03-06-2020 09:33:24; HG3456
07-06-2020 15:25:10; CH4747
10-06-2020 08:49:18; EU4821
12-06-2020 07:13:57; PP3478
我有一个日期时间格式,如下例所示,我想用 AWK 将其转换为 dd-mm-yyyy hh:mm:ss
。我该怎么做?
当前格式:
3Jun2020 9:33:24; HG3456
7Jun2020 15:25:10; CH4747
10Jun2020 8:49:18; EU4821
12Jun2020 7:13:57; PP3478
期望的输出:
03-06-2020 09:33:24; HG3456
07-06-2020 15:25:10; CH4747
10-06-2020 08:49:18; EU4821
12-06-2020 07:13:57; PP3478
perl -MPOSIX -MDate::Parse -pe 's{^\S+\s+\S+(?=;)}{strftime("%d-%m-%Y %T", strptime($&))}e; s/^0//' file
我将按照以下方式使用 GNU AWK
完成此任务,令 file.txt
内容为
3Jun2020 9:33:24; HG3456
7Jun2020 15:25:10; CH4747
10Jun2020 8:49:18; EU4821
12Jun2020 7:13:57; PP3478
然后
awk '{sub(/Jan/,"-01-",);sub(/Feb/,"-02-",);sub(/Mar/,"-03-",);sub(/Apr/,"-04-",);sub(/May/,"-05-",);sub(/Jun/,"-06-",);sub(/Jul/,"-07-",);sub(/Aug/,"-08-",);sub(/Sep/,"-09-",);sub(/Oct/,"-10-",);sub(/Nov/,"-11-",);sub(/Dec/,"-12-",);print}' file.txt
输出
3-06-2020 9:33:24; HG3456
7-06-2020 15:25:10; CH4747
10-06-2020 8:49:18; EU4821
12-06-2020 7:13:57; PP3478
解释:用-01-
替换Jan
,用-02-
替换Feb
,用-03-
替换Mar
等等,然后print
。免责声明:如果您使用其他语言环境,代码可能需要调整。
(在 GNU Awk 5.0.1 中测试)
如果 sed
是一个选项,您可以 e
在替换中执行 date
命令。
$ sed "s/\([^;]*\)\(.*\)/\date -d '' '+%d-%m-%Y %T'/e" input_file
03-06-2020 09:33:24; HG3456
07-06-2020 15:25:10; CH4747
10-06-2020 08:49:18; EU4821
12-06-2020 07:13:57; PP3478
在每个 Unix 机器上的任何 shell 中使用任何 awk:
$ cat tst.awk
BEGIN { FS=OFS=";" }
{
split(,t,/[ :]/)
lgth = length(t[1])
dayNr = substr(t[1],1,lgth - 7)
mthAbbr = substr(t[1],lgth - 6,3)
mthNr = (index("JanFebMarAprMayJunJulAugSepOctNovDec",mthAbbr) + 2) / 3
yrNr = substr(t[1],lgth - 3)
= sprintf("%02d-%02d-%04d %02d:%02d:%02d", dayNr, mthNr, yrNr, t[2], t[3], t[4])
print
}
$ awk -f tst.awk file
03-06-2020 09:33:24; HG3456
07-06-2020 15:25:10; CH4747
10-06-2020 08:49:18; EU4821
12-06-2020 07:13:57; PP3478