0

我正在寻找从 scontrol 的输出中解析“持续时间”的帮助

1

scontrol show res root_89
ReservationName=root_89 StartTime=2012-11-07T14:22:21 EndTime=2012-11-08T16:22:21 Duration=02:00:00
   Nodes=host NodeCnt=1 Features=(null) PartitionName=(null) Flags=SPEC_NODES
   Users=root Accounts=(null) Licenses=(null) State=ACTIVE

2

scontrol show res root_95
ReservationName=root_95 StartTime=2013-02-06T16:00:00 EndTime=2013-02-10T06:40:00 Duration=3-14:40:00
    Nodes=host NodeCnt=1 Features=(null) PartitionName=(null) Flags=SPEC_NODES
    Users=root Accounts=(null) Licenses=(null) State=INACTIVE

如您所见,预订是否真的很长持续时间输出混乱我正在寻找格式'xxHH:mm:ss'

4

2 回答 2

2

在 Perl 中非常简单:

perl -pe's/Duration=\K(\d+)-(\d+)/$1*24+$2/e' myfile
于 2012-11-12T18:31:20.277 回答
1

这条线应该适用于您的情况:

grep -Po "(?<=Duration=)[0-9-:]*" <yourFile>|awk -F'-' 'NF==2{split($2,t,":");t[1]+=($1*24);print t[1]":"t[2]":"t[3];next;}1'

请注意,我有点懒,所以我使用 grep 过滤掉了持续时间值。它可以用awk来完成,以保存一个进程。如果性能不是您的问题,您可以将 grep 保留在那里。

测试

kent$  cat example
scontrol show res root_89
ReservationName=root_89 StartTime=2012-11-07T14:22:21 EndTime=2012-11-08T16:22:21 Duration=02:00:00
   Nodes=host NodeCnt=1 Features=(null) PartitionName=(null) Flags=SPEC_NODES
   Users=root Accounts=(null) Licenses=(null) State=ACTIVE

scontrol show res root_95
ReservationName=root_95 StartTime=2013-02-06T16:00:00 EndTime=2013-02-10T06:40:00 Duration=3-14:40:00
    Nodes=host NodeCnt=1 Features=(null) PartitionName=(null) Flags=SPEC_NODES
    Users=root Accounts=(null) Licenses=(null) State=INACTIVE

kent$  grep -Po "(?<=Duration=)[0-9-:]*" example|awk -F'-' 'NF==2{split($2,t,":");t[1]+=($1*24);print t[1]":"t[2]":"t[3];next;}1'
02:00:00
86:40:00
于 2012-11-12T16:25:13.543 回答