这是一个脚本,可以让你走到一半:
#!/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
其余的......在运行这个脚本之后,每个 GO 行后面都会跟着一个具有相同 id 的 DONE 行,假设存在这样的 DONE 行。
接下来,您可以读取每对行,提取时间戳并区分它们(查看 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
我确信这很令人困惑,所以如果您有任何问题,请告诉我。我确信有更有效的方法来完成这一切,但这是我的想法。