我有以下文本檔案。
Account1,2h 01m 00s
Account2,4h 25m 23s
Account3,5h 43m 59s
我希望將小時、分鐘和秒的值相加,以便將它們合計到各自的分鐘總數中。
Account1 minute total = 121
Account2 minute total = 265
Account3 minute total = 343
我有以下 bash 檔案
cat data.txt | cut -f2 -d','
這隔離了時間值;但是,從這里我不知道我將采取哪些步驟來隔離時間,將其轉換為整數,然后將其轉換為分鐘。我曾嘗試使用 PARAM 但無濟于事。
uj5u.com熱心網友回復:
如果awk是一個選項,你可以試試這個
awk -F"[, ]" '{h=60; m=1; s=0.01666667}{split($2,a,/h/); split($3,b,/m/); split($4,c,/s/); print$1, "minute total = " int(a[1] * h b[1] * m c[1] * s)}' input_file
$ cat awk.script
BEGIN {
FS=",| "
} {
h=60
m=1
s=0.01666667
}{
split($2,a,/h/)
split($3,b,/m/)
split($4,c,/s/)
print $1, "minute total = " int(a[1] * h b[1] * m c[1] * s)
}
輸出
awk -f awk.script input_file
Account1 minute total = 121
Account2 minute total = 265
Account3 minute total = 343
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/317372.html
