这里有一个脚本,将让你中途有:
#!/bin/bash
# Script must be called with one parameter, the name of the file to process
if [ $# -ne 1 ]; then
echo "Usage: $0 filename"
exit
fi
filename=$1
# Use sed to put the timestamp after the id
# 10:46:01:0000 (id=20) GO
# 10:46:02:0000 (id=10) GO
# 10:46:03:0000 (id=10) DONE
# 10:46:04:0000 (id=20) DONE
#
# becomes
#
# (id=20) 10:46:01:0000 GO
# (id=10) 10:46:02:0000 GO
# (id=10) 10:46:03:0000 DONE
# (id=20) 10:46:04:0000 DONE
#
# \1 timestamp
# \2 id
# \3 status (GO or DONE)
# \1 \2 \3
sed -e "s/\([0-9:]*\) \((id=[0-9]*)\) \(.*\)/\2 \1 \3/" $filename > temp1
# Now sort the file. This will cause timestamps to be sorted, grouped by id
# (id=20) 10:46:01:0000 GO
# (id=10) 10:46:02:0000 GO
# (id=10) 10:46:03:0000 DONE
# (id=20) 10:46:04:0000 DONE
#
# becomes
#
# (id=10) 10:46:02:0000 GO
# (id=10) 10:46:03:0000 DONE
# (id=20) 10:46:01:0000 GO
# (id=20) 10:46:04:0000 DONE
sort temp1 > temp2
# Use sed to put the id after the timestamp
# (id=10) 10:46:02:0000 GO
# (id=10) 10:46:03:0000 DONE
# (id=20) 10:46:01:0000 GO
# (id=20) 10:46:04:0000 DONE
#
# becomes
#
# 10:46:02:0000 (id=10) GO
# 10:46:03:0000 (id=10) DONE
# 10:46:01:0000 (id=20) GO
# 10:46:04:0000 (id=20) DONE
# \1 id
# \2 timestamp
# \3 status (GO or DONE)
sed -e "s/\((id=[0-9]*)\) \([0-9:]*\) \(.*\)/\2 \1 \3/" temp2 > temp3
至于其他...运行此脚本后,各走各的线之后,将具有相同ID的DONE线,假设这样的一个DONE行存在。
接着可以读取每对线,提取时间戳和diff的它们(检查出的时间戳函数Johnsyweb建议的)。然后将两条线合并成一条线。您的结果现在看起来如下所示:
# 1s 10:46:02:0000 (id=10) GO 10:46:03:0000 (id=10) DONE
# 3s 10:46:01:0000 (id=20) GO 10:46:04:0000 (id=20) DONE
请注意,条目在起始时间戳记中是如何排序的。发生这种情况是因为我们之前按id排序。我将把它作为练习,让你了解如何以正确的顺序获取参赛作品。我们希望为ID = 20来ID = 10之前进入,因为ID = 20是ID = 10之前开始。
# 3s 10:46:01:0000 (id=20) GO 10:46:04:0000 (id=20) DONE
# 1s 10:46:02:0000 (id=10) GO 10:46:03:0000 (id=10) DONE
我敢肯定,这是令人困惑的,所以让我知道如果你有问题。我敢肯定,有更高效的方法可以做到这一切,但是这是我想到了我的头顶。
如果你有机会获得GNU的awk的