使用unix命令在日志文件中添加Diff时间戳
我有一个日志文件,其中的行如下:使用unix命令在日志文件中添加Diff时间戳,unix,timestamp,logfiles,Unix,Timestamp,Logfiles,我有一个日志文件,其中的行如下: ...timestamp...(id=1234)..GO... ...timestamp...(id=1234)..DONE... 事实: 时间戳的形式为HH:MM:SS.ssss(s表示部分秒) 每个“id”编号都有两个关联行,一个“开始”和一个“完成” 两条关联线不一定相邻;文件是按时间顺序排列的 我想要的是: 匹配相关的开始/完成行 区分时间戳 (理想情况下)创建表单的新文件: diffTime <GO line> <DONE l
...timestamp...(id=1234)..GO...
...timestamp...(id=1234)..DONE...
事实:
- 时间戳的形式为HH:MM:SS.ssss(s表示部分秒)
- 每个“id”编号都有两个关联行,一个“开始”和一个“完成”
- 两条关联线不一定相邻;文件是按时间顺序排列的
- 匹配相关的开始/完成行
- 区分时间戳
- (理想情况下)创建表单的新文件:
diffTime <GO line> <DONE line>
diffTime
我的主要症结在于区分时间戳。这将非常有用,而且我缺乏编写它的sort/sed/awk技能。是否有日志文件工具可以帮助进行此类黑客攻击?以下是一个脚本,它可以帮助您实现目标:
#!/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
我相信这会让人困惑,所以如果你有问题,请告诉我。我确信有更有效的方法来完成这一切,但这正是我头脑中的想法。这里有一个脚本,可以让你走到一半:
#!/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
我相信这会让人困惑,所以如果你有问题,请告诉我。我确信有更有效的方法来完成这一切,但这正是我头脑中的想法。我不知道任何这样的工具,但可以在shell中编写它。例如,此日志: 11:18:51 (id=123) GO 11:18:52 (id=124) GO 11:18:53 (id=123) DONE 11:18:54 (id=125) GO 11:18:55 (id=125) DONE 11:18:55 (id=124) DONE 这一行可能会被进一步简化 工作原理:
- 将行格式更改为“11:18:51 123 GO”
- 将GO替换为1,将DONE替换为2(因为在后面的部分中,它允许我们对其进行正确排序)
- 按事务id和状态对结果行排序
- 连接每两行(现在每个结果行描述事务的开始和结束)
- 将所有冒号替换为空格(以简化以后的扩展)
- 通过手动除法计算时间差
- 打印结果
- 将行格式更改为“11:18:51 123 GO”
- 将GO替换为1,将DONE替换为2(因为在后面的部分中,它允许我们对其进行正确排序)
- 按事务id和状态对结果行排序
- 连接每两行(现在每个结果行描述事务的开始和结束)
- 将所有冒号替换为空格(以简化以后的扩展)
- 通过手动除法计算时间差
- 打印结果
- 我不知道任何这样的工具,但可以在shell中编写。例如,此日志:
11:18:51 (id=123) GO
11:18:52 (id=124) GO
11:18:53 (id=123) DONE
11:18:54 (id=125) GO
11:18:55 (id=125) DONE
11:18:55 (id=124) DONE
这一行可能会被进一步简化
工作原理:
sed
步骤,直接按第二个字段排序:sort-k2…
。此外,我还将添加-s
选项,以使排序稳定,以防有多个具有相同id和时间戳的条目。您可以取消第一个sed
步骤,直接按第二个字段排序:sort-k2…
。此外,我还将添加-s
选项以使排序稳定,以防有多个具有相同id和时间戳的条目。