在指定的时间范围内提取日志文件数据 [英] extract data from log file in specified range of time
问题描述
我想提取使用基于时间段的shell脚本(bash)的日志文件的信息。在日志文件中的一行是这样的:
I want to extract information from a log file using a shell script (bash) based on time range. A line in the log file looks like this:
172.16.0.3 - - [31/Mar/2002:19:30:41 +0200] "GET / HTTP/1.1" 200 123 "" "Mozilla/5.0 (compatible; Konqueror/2.2.2-2; Linux)"
我想提取数据的特定时间间隔。例如,我只需要在在最后X分钟或X天即发生前从最后记录的数据的事件看。我在shell脚本新的,但我曾尝试使用grep命令。
i want to extract data specific intervals. For example I need to look only at the events which happened during the last X minutes or X days ago from the last recorded data. I'm new in shell scripting but i have tried to use grep command.
推荐答案
使用grep和常规的前pressions,例如,如果你想要的日志4分钟间隔:
Use grep and regular expressions, for example if you want 4 minutes interval of logs:
grep "31/Mar/2002:19:3[1-5]" logfile
将返回31 /月/ 2002 19:31和19:35之间的所有日志行。
假如你需要的最后5天,从今天开始,27 /月/ 2011,你可以使用以下内容:
will return all logs lines between 19:31 and 19:35 on 31/Mar/2002. Supposing you need the last 5 days starting from today 27/Sep/2011 you may use the following:
grep "2[3-7]/Sep/2011" logfile
这篇关于在指定的时间范围内提取日志文件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!