让我先说一句,我不知道这个问题以前是否有人问过,我一直在谷歌上搜索答案,但结果都很短。
我希望使用标准的Linux/Unix命令(在FreeBSD上运行此命令)将符合模式的日志文件中的行排除在外。日志文件还包括“最后一条消息重复了x次”来压缩日志条目。
举个例子,我想举个例子:
May 27 2023 11:07 relevant information #1
May 27 2023 11:07 relevant information #2
May 27 2023 11:08 last message repeated 3 times
May 27 2023 11:08 useless information #1
May 27 2023 11:08 last message repeated 5 times
May 27 2023 11:09 last message repeated 8 times
May 27 2023 11:09 relevant information #3
May 27 2023 11:09 useless information #2
May 27 2023 11:10 useless information #3
May 27 2023 11:10 last message repeated 6 times得到这个输出:
May 27 2023 11:07 relevant information #1
May 27 2023 11:07 relevant information #2
May 27 2023 11:08 last message repeated 3 times
May 27 2023 11:09 relevant information #3我已经使用sed命令来完成这个任务了,但是我还不知道它是如何工作的。当涉及到有多条“最后一条消息重复”的日志行时,我尤其感到迷茫。下面是我目前正在做的工作:
sed '/useless information/{d;N;/last message repeated/d;}' ./logfile.txt上面的代码首先删除包含“无用信息”的匹配行,然后使用N将下一行添加到命名空间,然后如果它包含“重复的最后一条消息”,则应该删除生成的行。但它只是用“无用的信息”删除了这些行。
发布于 2023-05-28 23:47:28
如果您可以测试相关的而不是无用的信息,那么在每个Unix框上使用任何shell中的任何awk:
awk '/last message repeated/ && f; {f=/relevant information/} f' file
May 27 2023 11:07 relevant information #1
May 27 2023 11:07 relevant information #2
May 27 2023 11:08 last message repeated 3 times
May 27 2023 11:09 relevant information #3https://unix.stackexchange.com/questions/747272
复制相似问题