Linux

將 mm 轉換為 hh:mm

  • February 22, 2022

awk用來獲取所有第 4 個逗號分隔的欄位,這是一個以分鐘為單位的持續時間。所需的輸出格式為 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

使用格式化列印功能,如

> 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

使用您的腳本:

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

以這種方式改變它:

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

引用自:https://unix.stackexchange.com/questions/691171