【问题标题】:awk filter out messages/rules and display number of merged messagesawk 过滤掉消息/规则并显示合并消息的数量
【发布时间】:2018-09-24 13:35:04
【问题描述】:

对于我工作的公司,我想过滤掉(实际上:2)日志文件中的某些消息。

这些消息只是提供信息,在排除错误/故障时并不是特别有用。

经过长时间的考虑(我也发布了一个类似的问题,但对于 Windows 和它的 PS/BS(某种“牛粪”;))

我认为 AWK 适合这项工作,并且我制作了一个 shell 脚本。 但是,它没有运行(预期)。 有人可以帮我“填空”吗?

#!/bin/bash

## URL that could have been the answer (but not quite)    https://stackoverflow.com/questions/10842118/explain-this-duplicate-line-removing-order-retaining-one-line-awk-command



###To sort by what you WANT to see:
##e.g awk '/term to search/' dpkg.log

#if 
#    $var_show awk '/installed/' syslog/dpkg.log
#    then
#    printf('$var_show')
#fi







##Show what DONT want to see.
if
    #$var_notshow awk /'what not to display'/ syslog/dpkg.log
    $var_notshow awk /'Status Installed'/ dpkg.log
then
wc -1 > $var_notshow 
#echo number of merged messages (of the same content): xxx merged messages #< is the amount 
echo Messages of Status installed: $var_notshow were merged
fi 
###!!Show the amount of rules (when the same rule/logged event) that were merged
## E.g. (multiple lines which state: "Status Installed: xxxxxxxxxxxxx" ) and display it as: "Messages of Status Installed: xxxx were merged. Totalling: # of messages (of Status Installed)" were merged. 

### Finally, save it in a different file.
#Like: ?? how to do that?

所以,基本上我有两种方法:过滤你想要的或你不想要的。开始过滤掉我不想要的消息可能会更好/更干净。

是的,作为概念证明,我在我的测试机中使用了标准日志文件。 我可以将其转换为公司特定信息...

日志文件摘录:

 11:56:31 status half-configured grep:amd64 3.1-2
 11:56:32 status installed grep:amd64 3.1-2
 11:56:32 configure debconf:all 1.5.66 <none>
 11:56:32 status unpacked debconf:all 1.5.66
 11:56:32 status unpacked debconf:all 1.5.66
 11:56:32 status unpacked debconf:all 1.5.66
 11:56:32 status half-configured debconf:all 1.5.66
 11:56:32 status installed debconf:all 1.5.66
 11:56:32 configure gzip:amd64 1.6-5ubuntu1 <none>
 11:56:33 status half-configured util-linux:amd64 2.31.1-0.
 11:56:34 status installed util-linux:amd64 2.31.1-0.4ubuntu3
 11:56:34 configure libpam-modules-bin:amd64 1.1.8-3.6ubuntu2 <none>
 11:56:34 status unpacked libpam-modules-bin:amd64 1.1.8-3.6ubuntu2
 11:56:34 status half-configured libpam-modules-bin:amd64 1.1.8-3.6ubuntu2
 11:56:34 status installed libpam-modules-bin:amd64 1.1.8-3.6ubuntu2
 11:56:34 configure mount:amd64 2.31.1-0.4ubuntu3 <none>
 11:56:34 status unpacked mount:amd64 2.31.1-0.4ubuntu3
 11:56:34 status half-configured mount:amd64 2.31.1-0.4ubuntu3
 11:56:34 status installed mount:amd64 2.31.1-0.4ubuntu3
 11:56:34 configure procps:amd64 2:3.3.12-3ubuntu1 <none>
 11:56:34 status unpacked procps:amd64 2:3.3.12-3ubuntu1
 11:56:34 status unpacked procps:amd64 2:3.3.12-3ubuntu1
 11:56:34 status unpacked procps:amd64 2:3.3.12-3ubuntu1

提前谢谢:)

托马斯

【问题讨论】:

  • 听起来像awk 可以做的事情。请分享一些示例数据和您想要的 awk 输出,否则我不确定我们是否能提供更多帮助,而不是同意您选择 awk
  • 很高兴它可以。我对日志文件做了一小段摘录(test-vm 的标准 dpkg.log)。可在:pastebin.com/bnri9gfa 获得。以及我想从 awkscript 中显示的内容:找到 23 条日志线(
  • 你是什么意思“Awk 将采取完整的路线,而不仅仅是“状态已安装””。通过这 23 行示例,您能否准确地提供您希望 awk 吐出的内容,如果有任何不明显的逻辑可以实现,也分享一下。 “Take”和“Merge”是我真正喜欢的地方。我不知道这两个术语对 awk 的含义。
  • 另一个混淆点是您发布的代码。它与工作相距甚远,并且充满了注释掉的代码和未注释掉的代码(绝对不会做你想做的事情),它对这个问题没有帮助。请使用此示例更好地解释您想要的输出。这里没有什么有意义的线索。
  • @ThomasAnoniem 找到有用的东西了吗?

标签: shell awk filter line rules


【解决方案1】:

显示所有有趣的行:

grep interesting file

显示除不感兴趣的行之外的所有行:

grep -v "Status uninteresting" file

用 awk 计数:

awk '/uninteresting/{n++}END{print "uninteresting messages: "n}'

将命令输出重定向到新文件:

grep interesting file | grep -v uninteresting > newFile

或附加到新文件:

grep interesting file | grep -v uninteresting >> newFile

一次做所有事情:

awk '/uninteresting/{u++;next}/interesting/{print}END{print "uninteresting lines: "u}'
this is interesting
uninteresting lines: 1

【讨论】:

  • 有机会我会试试这个。谢谢史蒂芬
猜你喜欢
  • 2021-05-11
  • 1970-01-01
  • 2021-05-21
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 2014-11-30
  • 2011-12-14
  • 2019-09-17
相关资源
最近更新 更多