将毫米转换为 hh:mm

将毫米转换为 hh:mm

我用来awk获取所有第四个逗号分隔字段,其持续时间以分钟为单位。所需的输出格式为 hh:mm,因此我需要将其从分钟转换。

"jone","spdb-deploy","SD",25
"abrham","Data-deploy","DD",5
"crater","aps-deploy","AD",25
"valu","Platform-deploy","PD",5
"kiol","teer-deploy","TD",180
$ cat dat.csv | awk -F',' '{print $4}'
25
5
25
5
180

...以上值以分钟为单位,我希望将这些值打印为 hh:mm ,如下所示:

00:25
00:05
00:25
00:05
03:00

答案1

使用您的脚本:

awk -F',' '{print $4}' dat.csv

按这种方式更改它:

awk -F',' '{h=int($4/60);m=$4%60;printf "%02d:%02d\n", h,m}' dat.csv

答案2

使用格式化打印功能,例如

> awk -F, '{$4 = sprintf("%02d:%02d", int($4/60), $4%60)}1' OFS=, file
"jone","spdb-deploy","SD",00:25
"abrham","Data-deploy","DD",00:05
"crater","aps-deploy","AD",00:25
"valu","Platform-deploy","PD",00:05
"kiol","teer-deploy","TD",03:00

答案3

如果你使用 cat dat.csv | awk -F',' '{printf("%02d:%02d\n", $4/60, $4%60)}' 它应该可以工作。这样做的好处是,如果您已经了解 C 的 printf,那么很容易理解它在做什么。

相关内容