在csv文件的第3列中将秒转换为年龄

时间:2015-11-10 16:18:04

标签: bash awk seconds

以逗号分隔的csv文件的第3列包含我想要转换为age的秒数。 (以下样本数据)。我正在使用bash生成这些报告,并希望将秒转换为年龄,如10d 3h 20min 30sec或类似。有一个简单的方法吗?

由于

28126265
6678363
3182862
11401914
1029092
9655690
34381431
13126178
2516335

4 个答案:

答案 0 :(得分:1)

使用Perl:

perl -F, -ane 'printf "%dd %dh %dm %ds\n", int($F[2]/(24*60*60)), ($F[2]/(60*60))%24, ($F[2]/60)%60, $F[2]%60' file

-a autosplits每行到数组@F
-F,使用逗号作为分隔符来自动化每一行 由于Perl数组从索引0开始,因此第3个元素是$F[2]

输入:

a,b,28126265,d,e
a,b,6678363,d,e
a,b,3182862,d,e
a,b,11401914,d,e
a,b,1029092,d,e
a,b,9655690,d,e
a,b,34381431,d,e
a,b,13126178,d,e
a,b,2516335,d,e

输出:

325d 12h 51m 5s
77d 7h 6m 3s
36d 20h 7m 42s
131d 23h 11m 54s
11d 21h 51m 32s
111d 18h 8m 10s
397d 22h 23m 51s
151d 22h 9m 38s
29d 2h 58m 55s

由于您要保留现有数据:

perl -F, -ane 'printf "%s,%dd %dh %dm %ds,%s", (join ",",@F[0..1]), int($F[2]/(24*60*60)), ($F[2]/(60*60))%24, ($F[2]/60)%60, $F[2]%60, (join ",",@F[3..$#F])' file

@F[0..1]是数组@F的前两个元素的数组切片 $#F是数组@F

的最后一个元素的索引

输出:

a,b,325d 12h 51m 5s,d,e
a,b,77d 7h 6m 3s,d,e
a,b,36d 20h 7m 42s,d,e
a,b,131d 23h 11m 54s,d,e
a,b,11d 21h 51m 32s,d,e
a,b,111d 18h 8m 10s,d,e
a,b,397d 22h 23m 51s,d,e
a,b,151d 22h 9m 38s,d,e
a,b,29d 2h 58m 55s,d,e

或者替代:

perl -F, -ane '$F[2] = sprintf "%dd %dh %dm %ds", int($F[2]/(24*60*60)), ($F[2]/(60*60))%24, ($F[2]/60)%60, $F[2]%60; print join ",",@F' file

答案 1 :(得分:0)

这是一种可能的解决方案。但不是一个oneliner。

time=28126265
m=60
h=$((m * 60))
d=$((24 * h))
nd=$((time / d))
rt=$((time % d))
nh=$((rt / h))
rt=$((rt % h))
nm=$((rt / m))
rt=$((rt % m))
echo ${nd}d ${nh}h ${nm}m ${rt}s  

答案 2 :(得分:0)

除非您愿意使用Perl或Python,否则它不会是单行内容。

在awk中,(我不擅长)这里是一个解决方案。

假设:

$ echo "$ages"
28126265
6678363
3182862
11401914
1029092
9655690
34381431
13126178
2516335

你可以这样做:

echo "$ages" | awk '{
t=int($0);
days=int(t/(24*60*60));
t-=days*24*60*60;
hours=int(t/(60*60));
t-=hours*60*60;
minutes=int(t/60);
seconds=int(t-minutes*60);
printf("%dd %02dh %02dm %02ds\n", days, hours, minutes, seconds);}'

打印:

325d 12h 51m 05s
77d 07h 06m 03s
36d 20h 07m 42s
131d 23h 11m 54s
11d 21h 51m 32s
111d 18h 08m 10s
397d 22h 23m 51s
151d 22h 09m 38s
29d 02h 58m 55s

答案 3 :(得分:0)

这是另一种方式

awk -v FS="[,]+" '{days=$3/86400;hours=(days-int(days))*24;minutes=(hours-int(hours))*60;seconds=(minutes-int(minutes))*60;print int(days)"d",int(hours)"h",int(minutes)"m",int(seconds)"s"}'

这是细分

awk -v FS="[,]+" '{
days=$3/86400; #find floating value for days
hours=(days-int(days))*24; #minus floating value from rounded value and multiply by 24 to get floating value for hours
minutes=(hours-int(hours))*60; #minus floating value of hours from rounded value and multiply by 60 to get floating value for minutes
seconds=(minutes-int(minutes))*60; #minus floating value of minutes from rounded value and multiply by 60 to get floating value for seconds
print int(days)"d",int(hours)"h",int(minutes)"m",int(seconds)"s"
}'

输出

325d 12h 51m 4s
77d 7h 6m 3s
36d 20h 7m 41s
131d 23h 11m 53s
11d 21h 51m 32s
111d 18h 8m 10s
397d 22h 23m 50s
151d 22h 9m 37s
29d 2h 58m 55s