如何使用 grep 或 awk、pattern 计算文件最后 10 行中分别出现 x 次(一个接一个)

发布于 2024-11-05 23:49:28 字数 3541 浏览 0 评论 0原文

我需要一些代码的帮助。我需要的是“if 语句”代码,用 grep 或其他命令分别计算文件中两个单词重复的次数。 文件的竞争是:

2011/05/04 21:47:37 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - CHANNEL 1 (0,8W) (invalid SID)
2011/05/04 21:47:46 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:0000 unknown (invalid SID)
2011/05/04 21:48:11 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL
2011/05/04 21:48:21 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:48:42 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:1000 unknown (invalid SID)
2011/05/04 21:49:19 45992299 c blabla (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:49:30 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL CHANNEL (0,8
2011/05/04 21:49:32 45992299 c blabla (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:50:01 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:50:18 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:0333 unknown (invalid SID)
2011/05/04 21:50:21 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:0333 unknown (invalid SID)
2011/05/04 21:51:06 45992299 c blabla (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:51:08 45992299 c blabla (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:51:18 45992299 c blabla (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL
2011/05/04 21:52:19 45992299 c blabla (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:52:45 45992299 c blabla (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:53:56 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:53:58 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:01 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:04 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:07 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:10 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:13 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:15 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:18 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2011 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:20 45992299 c blabla (xxx/xxx/xx:xxx): not found (2763 ms) by blablabla (of 1 avail 1) - CHANNEL (0.8W)
2011/05/04 21:54:29 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)

如果单词(模式)“未找到”在每一行中逐一重复(如示例文件中),并且如果达到 10 次,但在访问文件时它是最后 10 行日志文件然后做一些事情(我将输入我的命令)

这部分代码是否有可能实时监控当前文件? 我的意思是如果用 crond 启动一次来持续监视我的文件?

谢谢帮助!

I need help with little code. What I need is "if statement" code the counts with grep or other command how many times repeat respectively two words in the file.
The contest of file is:

2011/05/04 21:47:37 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - CHANNEL 1 (0,8W) (invalid SID)
2011/05/04 21:47:46 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:0000 unknown (invalid SID)
2011/05/04 21:48:11 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL
2011/05/04 21:48:21 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:48:42 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:1000 unknown (invalid SID)
2011/05/04 21:49:19 45992299 c blabla (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:49:30 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL CHANNEL (0,8
2011/05/04 21:49:32 45992299 c blabla (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:50:01 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:50:18 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:0333 unknown (invalid SID)
2011/05/04 21:50:21 45992299 c blabla (xxx/xxx/xx:xxx): invalid (0 ms) (of 0 avail 0) - 0000:0333 unknown (invalid SID)
2011/05/04 21:51:06 45992299 c blabla (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:51:08 45992299 c blabla (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:51:18 45992299 c blabla (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL
2011/05/04 21:52:19 45992299 c blabla (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:52:45 45992299 c blabla (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:53:56 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:53:58 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:01 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:04 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:07 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:10 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2010 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:13 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2009 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:15 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2007 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:18 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2011 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)
2011/05/04 21:54:20 45992299 c blabla (xxx/xxx/xx:xxx): not found (2763 ms) by blablabla (of 1 avail 1) - CHANNEL (0.8W)
2011/05/04 21:54:29 05002299 c blabla2 (xxx/xxx/xx:xxx): not found (2008 ms) by blablabla (of 1 avail 1) - CHANNEL (0,8W)

If words (pattern) "not found" repeat in each line one after one (like in sample file) and if it reach 10 times, but it is a last 10 line in moment when accessing the log file then DO SOMETHING (I will put my command)

Is there any posibility that this part of code monitoring in real time current file??
I mean if start it once with crond to continuously monitoring my file?

Thx for help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-11-12 23:49:28

您可以尝试以下操作:

tail -n 10 -f <filename> | grep -c "not found"

这应该会显示文件最后 10 行中包含 not find 的行数。然后,您可以将其设置为按时间间隔运行,如果计数是您想要的,则可以执行任何您想要的操作。

You can try this:

tail -n 10 -f <filename> | grep -c "not found"

That should give you the count of lines in the last 10 lines of the file that contain not found. You could then set it up to run at timed intervals and do whatever you want if the count is what you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文