简体   繁体   English

如何使用awk / sed / grep仅过滤掉实时日志文件中的最新字符串

[英]How to use awk/sed/grep to filter out only the latest string in a real-time log file

this thing is really confusing me. 这件事真的让我感到困惑。 Suppose I have a file like this 假设我有一个像这样的文件

09:35:24 03/04/2014  Pink Floyd the best band in the world KING KING
09:44:21 03/04/2014 Led Zeppelin the greatest hard rock band ever 
09:54:21 03/04/2014 Bon scott I love Bon scott KING KING
10:15:23 03/04/2014 AC/DC is the best
10:35:43 03/04/2014 It's all a joke 
12:46:55 03/04/2014 No value nothing is there here KING KING 
15:35:43 03/04/2014 It's all a joke1 
15:39:43 03/04/2014 It's all a joker KING KING
15:55:43 03/04/2014 It's all a jokeeyyyy 

Now the above one is a file which will be created in the morning and it keeps appending till midnight. 现在,上面的文件是一个文件,该文件将在早上创建,并一直追加到午夜。 Now I have to extract lines where the pattern KING KING exists. 现在,我必须提取模式KING KING存在的行。 Mine is a monitoring script so it checks this file every 60s for the pattern KING KING and if it exists I shall put it in a new file for further operations. 我的是一个监视脚本,因此它每60秒检查一次该文件是否为KING KING模式,如果存在,我将其放在新文件中以进行进一步操作。 Now if you see timestamps are different.At 10:15 I got the first 2 lines with the matching pattern. 现在,如果您看到时间戳是不同的,则在10:15,我得到了前两行具有匹配模式的行。 Now at 12:46 when I run the script I don't want the previously found Matching Pattern again I,e at 12:46 I only want "12:46:55 03/04/2014 No value nothing is there here KING KING " and not again the values at 09:54 and 09:35 (I,e Bon scott and Pink Floyd) So basically how do I use grep/sed/awk to filter only the latest and not give me the old ones. 现在在12:46运行脚本时,我不再想要以前找到的匹配模式,即在12:46我只想要"12:46:55 03/04/2014 No value nothing is there here KING KING "而不是09:54和09:35的值(我是Bon scott和Pink Floyd),因此基本上,我该如何使用grep / sed / awk仅过滤最新的值,而不给我旧的值。 Thanks. 谢谢。

EDIT: 编辑:

at 10:00 My script runs I grep for KING KING and get the Pink Floyd and Bon Scott lines. 在10:00,我的脚本运行KING KING的grep,并获得Pink Floyd和Bon Scott的台词。 Now again (say) at 16:00 My script runs I only want the strings 现在再说一次(比如说)16:00我的脚本运行了,我只想要字符串

12:46:55 03/04/2014 No value nothing is there here KING KING

and

15:39:43 03/04/2014 It's all a joker KING KING

and not the older ones. 而不是较老的。 Please note there can be many occurrences of KING KING pattern not only 2. 请注意,不仅2,而且可能有很多KING KING模式。

grep "KING KING" <your file> | tail -n 1

You said you only wanted the most recent line containing KING KING. 您说您只想要包含KING KING的最新行。 You can use tail -n 1 to grab just the last one, which is the most recent one assuming your file appends, not prepends, lines. 您可以使用tail -n 1来抓取最后一个,这是最新的一个,前提是您的文件追加而不是添加行。

If it prepends you can use head instead. 如果准备好了,您可以改用head

EDIT: 编辑:

If you would like to look at the potentially new KING KING lines that may come in as your original log updates, you can use tail -f instead up there. 如果您想查看原始日志更新中可能出现的潜在新KING KING行,则可以在此处使用tail -f

Otherwise, if you want just the latest one but you want it to update, you're going to have to keep polling it over and over. 否则,如果您只想要最新的,但又想更新,则将不得不不断地对其进行轮询。

Example: 例:

while true
do
    grep "KING KING" <your file> | grep -vf <your temporary file>
    grep "KING KING" <your file> > <your temporary file>
    sleep 2
done

That's just off the top of my head. 那只是我的头上。 It might need some tweaks but I don't have access to a terminal right now. 它可能需要一些调整,但我现在无法访问终端。 Also, you don't need to rewrite temp file every time, and you can use cat to just append to the end of your temp file but I'll leave it to you to work that one out though. 另外,您不需要每次都重写临时文件,并且可以使用cat追加到临时文件的末尾,但是我将留给您使用,以解决该问题。 :) :)

You could keep a temporary file with the day's previous matches in: 您可以将一个临时文件保存在当天的先前比赛中:

touch matches.txt
grep "KING KING" output.log | grep -vFf matches.txt | tee -a matches.txt

This will filter out any previous matches stored in matches.txt , and also appends the new matches to that file. 这将过滤掉matches.txt中存储的所有先前匹配项,并将新匹配项追加到该文件。 You will need to empty this at midnight, presumably. 您可能需要在午夜将其清空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM