如何使用 grep 或 awk、pattern 计算文件最后 10 行中分别出现 x 次(一个接一个)
我需要一些代码的帮助。我需要的是“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试以下操作:
这应该会显示文件最后 10 行中包含 not find 的行数。然后,您可以将其设置为按时间间隔运行,如果计数是您想要的,则可以执行任何您想要的操作。
You can try this:
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.