AI Powered grep and awk

费流版本,看这个,很详细[1]


下面是自研版本:

坏了,有了 AI,感觉 grepawk 更不用刻意去学了,直接给 AI 说,命令用完就丢完全不用过脑子的感觉太爽了。然后用 matplotlib 画图,它都能行!

还是过下脑子吧。

假设数据就是日志(日志的格式也很重要呢):

1
2
3
4
5
6
7
8
9
10
[LOG] App input: 5.729685 output value: 2.176583
[LOG] App input: 6.278612 output value: 7.013304
[LOG] App input: 1.878629 output value: 0.104640
[LOG] App input: 9.102657 output value: 0.226886
[LOG] App input: 0.890096 output value: 4.717943
[LOG] App input: 0.504433 output value: 9.779706
[LOG] App input: 6.681956 output value: 1.431356
[LOG] App input: 2.422977 output value: 6.832860
[LOG] App input: 7.745818 output value: 1.674912
[LOG] App input: 3.779284 output value: 1.717913

看看 awk 的命令参数:

1
awk options 'pattern {action}' file

options:

  • -F fs --field-separator=fs:指定列分隔符,默认是空格 ' '
  • -f progfile -file=progfile:指定输入的脚本内容,用于 'pattern {action}' 很长的场景。

'pattern {action}':就是要执行的脚本内容,可以通过 -f 参数传递替换。具体能力就具体学习了,可以参考一下别的地方[1]

file:要处理的文本文件

讲讲脚本/命令,中的一些函数和变量:

VariableExplain
NRNumber of records,当前读取的总记录数量,处理一个文件时候,等价于 FNR
FNRFile number of records,当前记录在当前文件中的行数
NFNumber of field,当前记录的字段数量
$0当前记录
$x这里 x 是自然数,表示当前行记录的第 x 个字段
FunctionExplain
print打印, { print $2, $NF }
printf打印,带格式 { printf("The 2nd field is %d", $2)}

好啦你已经掌握了怎么提取数据啦,那么试试吧:

1
awk -F":" '{print NR $NF}' tmp.txt
1
2
3
4
5
6
7
8
9
10
1 2.176583
2 7.013304
3 0.104640
4 0.226886
5 4.717943
6 9.779706
7 1.431356
8 6.832860
9 1.674912
10 1.717913

太好了,然后把数据丢到 matplotlib 里面就行啦,拜拜~