如何使用grep或AWK,图案计数是否发生分别x次(一前一后) [英] How to count with grep or awk, pattern if it occurred x times respectively (one after the other)

查看:144
本文介绍了如何使用grep或AWK,图案计数是否发生分别x次(一前一后)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要很少code帮助。我需要的是if语句code使用grep或其他命令多少次在文件中分别重复两个字计数。
文件的较量是:

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)

如果字(图案)未找到重复每行一个一(就像示例文件),如果达到10次,然后做一些事情(我将把我的命令)

If words (pattern) "not found" repeat in each line one after one (like in sample file) and if it reach 10 times, then DO SOMETHING (I will put my command)

有没有posibility是的code监视这一部分的实时当前文件?
我的意思是如果与crond的一次启动它来持续监控我的档案?

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?

修改
现在我明白了,我需要的东西比别人是否更脚本..
如何实现我的剧本看所有的时间(实时)从文件最后10行?

EDIT Now I see that I need something more in script than if else.. How to implement to my script watching all time (realtime) last 10 line from file?

THX的帮助!

推荐答案

这AWK code打印出消息,当且仅当有10个或更多的连续线,未找到

This AWK code prints out the message if and only if there are 10 or more consecutive lines with "not found":

! /not found/ {
    if (count >= 10) {
        print "More than 10 lines found"
    }
    count = 0
}
/not found/ {
    ++count
}

END {
    if (count >= 10) {
        print "More than 10 lines found"
    }

这篇关于如何使用grep或AWK,图案计数是否发生分别x次(一前一后)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆