我有一个日志文件,我需要解析它以查找某个事件是否跟随另一个相关事件。基本上,第一个事件是单独的还是具有关联的配对事件。例如,数据可以是以下形式:
Timestamp Event Property1 Property2 Property3
1445210282416 E1 A 1 Type1 *
1445210282434 F1 D 3 Type10
1445210282490 E1 C 5 Type2
1445210282539 E2 A 1 Type1 *
1445210282943 F1 D 1 Type15
1445210285452 E2 C 4 Type3
这是一个简化的示例,但与数据文件基本相同。我们正在尝试查找某个事件E1
是否有相应的事件E2
Property1
,Property2
和Property3
但是在两个事件中是相同的*显示。第二个E1
事件(第3行)没有相应的E2
事件。我还需要对没有与Property3相对应的对的事件进行计数,作为以后使用的关键。
文件可能非常大(大约1 GB),并且应该避免将整个文件同时存储在内存中。所以,我想我可以使用发电机。
初步尝试是:
with open(filename, 'rb') as f:
finding_pair = 0 # indicator to help determine what to do in a line of the file
e1 = {} # store the E1 row whose pair we want to find
without_pair = {} # store count of E1 events with no pair
line = csv.DictReader((line for line in f), delimiter = ' ')
for l in line:
if l['Event'] = E1 and finding_pair = 0: # find pair for this
// Go through file after this line to find E2 event.
e1 = l
finding_pair = 1
elif (l['Event'] = E1 or l['Event'] = F1) and finding_pair = 1: # skip this and keep finding pair
continue
elif l['Event'] = E2 and finding_pair = 1: # see if this is a pair
if l['Property1'] == e1['Property1'] and l['Property2'] == e1['Property2'] and l['Property3'] == e1['Property3']:
# pair found
finding_pair = 0
// Go to next E1 line ??
else:
# pair not found
without_pair['Property3'] += 1
// Go to next E1 line ??
所以,我的问题是:
答案 0 :(得分:1)
脚本:基于将data
复制到pair.txr
并进行编辑以添加提取和输出指令。
$ cat pair.txr
Timestamp Event Property1 Property2 Property3
@ts1 E1 @p1 @p2 @p3
@(skip)
@(line ln)
@ts2 @e2 @p1 @p2 @p3
@(output)
Duplicate of E1 found at line @ln: event @e2 timestamp @ts2.
@(end)
执行命令
$ txr pair.txr data
Duplicate of E1 found at line 5: event E2 timestamp 1445210282539.
运行一些不匹配的数据:
$ txr pair.txr /etc/motd # failed termination status
$ echo $?
1
数据是:
$ cat data
Timestamp Event Property1 Property2 Property3
1445210282416 E1 A 1 Type1
1445210282434 F1 D 3 Type10
1445210282490 E1 C 5 Type2
1445210282539 E2 A 1 Type1
1445210282943 F1 D 1 Type15
1445210285452 E2 C 4 Type3
如果约束是第二个事件必须具有名称E2
,那么我们只需将e2
变量替换为文字文本E2
。
如果您知道副本必须在100行内,则可以使用@(skip 100)
。这可以防止浪费时间扫描没有重复的大文件。当然,100不必是恒定的;它可以计算。如果有多个重复项,@(skip :greedy)
将找到最后一个重复项。
请注意,尽管@(line ln)
本身就是一行,但它具有不消耗一行的语义。它将ln
变量绑定到输入中的当前行号,但不会前进到下一行,以便将模式语言的后续行应用于同一行。因此ln
表示该模式匹配的行。
现在,让我们做一些有趣的事情:让我们使用E1变量和第二个事件。此外,我们不要假设要匹配的事件是第一个:
Timestamp Event Property1 Property2 Property3
@(skip)
@ts1 @e1 @p1 @p2 @p3
@(skip)
@(line ln)
@ts2 @e2 @p1 @p2 @p3
@(output)
Duplicate of @e1 found at line @ln: event @e2 timestamp @ts2.
@(end)
目前,此代码现在只能在数据中找到第一个 a 对:
$ txr pair.txr data
Duplicate of E1 found at line 5: event E2 timestamp 1445210282539.
我们现在可以做的是从命令行限制变量,如下所示:
# Is there an E1 followed by a duplicate?
$ txr -De1=E1 pair.txr data
Duplicate of E1 found at line 5: event E2 timestamp 1445210282539.
# Is there an E2 followed by a duplicate?
$ txr -De1=E2 pair.txr data
$ echo $?
1
# Is there some event which is followed by a dupe called E2?
$ txr -De2=E2 pair.txr data
Duplicate of E1 found at line 5: event E2 timestamp 1445210282539.
# Is there a pair of duplicates whose Property3 is Type1?
$ txr -Dp3=Type1 pair.txr data
Duplicate of E1 found at line 5: event E2 timestamp 1445210282539.
你明白了。