有没有可以对输入流进行顶层化的工具?

tu-*_*duh 7 logging regex top stream

我遇到了很多用例,其中从(通常是换行符分隔的)流中获取输入并以类似 top 的方式对其进行总结非常有用(参见 top、iotop 等)。一种即时数据透视表。

例如采用对数式输入:

I heard A from unit 1 and it said "Great!" 56
I heard A from unit 2 and it said "Oh no!" 42
I heard C from unit 1 and it said "Waiting for input." 33
I heard B from unit 3 and it said "Stopped." -1
...
Run Code Online (Sandbox Code Playgroud)

由此,我们可以运行一个带有正则表达式和组指标的工具:

topify [lineout] [regex] [name #1] [group #1] [name #2] [group #2] [All other columns name position]
     where:
         lineout is the number of lines before removing it from the display
         regex is a regex of the lines to match, complete with group indicators
         name #n is a string for the title of column n
         group #n is the number of the group in the regex
Run Code Online (Sandbox Code Playgroud)

例如

topify '/^I heard ([A-Z]) from unit ([1-9]) and it said "(.*)" ([-0-9]*)$/' Unit 2 Status 1 Message 3 RetVal 4
Run Code Online (Sandbox Code Playgroud)

这将以交互方式显示,以便可以选择/重新排序列等:

Unit Status Message            Retval
1    C      Waiting for input. 33
2    A      Oh no!             42
3    B      Stopped.           -1
Run Code Online (Sandbox Code Playgroud)

我理解它的脆弱性,但如果它之前没有构建过,我会非常惊讶,并在我开始构建之前想检查一下。我也很欣赏它的编写并不是特别复杂,所以也许每个人都刚刚实施了自己的解决方案......

有没有人见过这样的工具?

(请原谅我在这里使用的标签。我知道我可能会推动/破坏某些标签的规则,但这是非常笼统的。欢迎提出建议。)

Aar*_*ate 1

你不需要写一个工具,标准的unix工具集就可以满足你的需要。

#!/bin/bash
echo -e 'Unit\tStatus\tMessage\t\t\tRetval'
cat /var/log/filename | awk '{match($0,"\".*\"",a)}{print $6 "\t" $3 "\t" a[0] "\t\t" $NF}' |sort -k<fieldnum>
Run Code Online (Sandbox Code Playgroud)

将其放入 .sh 文件中并对其运行 watch。