The logs I am processing is Hadoop log (log4j). It is in format like:
我正在处理的日志是Hadoop日志(log4j)。 格式如下:
2014-09-20 21:55:11,855 INFO org.apache.hadoop.nfs.nfs3.IdUserGroup: Updated user map size: 36
2014-09-20 21:55:11,863 INFO org.apache.hadoop.nfs.nfs3.IdUserGroup: Updated group map size: 55
2014-09-20 22:10:11,907 INFO org.apache.hadoop.nfs.nfs3.IdUserGroup: Updatecache now
2014-09-20 22:10:11,907 INFO org.apache.hadoop.nfs.nfs3.IdUserGroup: Not doing static UID/GID mapping because '/etc/nfs.map' does not exist.
Now, I want to get all the logs with a specific time range, e.g. last 4 hours. How to achieve this?
现在,我想获取具有特定时间范围(例如最近4个小时)的所有日志。 如何实现呢?
It should be with command line tools since it is in an automatic routine which is invoked by crond every 4 hours.
应该使用命令行工具,因为它处于crond每4小时调用一次的自动例程中。
You can use date to generate filtering rules to filter out the logs in a specific range:
您可以使用日期来生成过滤规则,以过滤出特定范围内的日志:
# grep out latest log
echo "" >$tmplog
for ((i=4; i>=1; i--)); do
grep "^$(date -d -${i}hour +'%Y-%m-%d %H')" $log >> $tmplog
done
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。
翻译自: https://www.systutorials.com/how-to-get-logs-of-a-specific-time-range-on-linux/