途中まで到達するスクリプトは次のとおりです。
#!/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
そして残りの部分...このスクリプトを実行した後、そのようなDONE行が存在すると仮定すると、各GO行の後に同じIDのDONE行が続きます。
次に、行の各ペアを読み取り、タイムスタンプを抽出して差分をとることができます(Johnsywebが提案したタイムスタンプ関数を確認してください)。次に、2つの行を1つの行に統合します。結果は次のようになります。
# 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
これは紛らわしいと思いますので、ご不明な点がございましたらお知らせください。これらすべてを行うためのより効率的な方法があると確信していますが、これは私が頭のてっぺんから考えたものです。