如何从日志文件中提取最后一小时的日志?
这是日志文件
2017-05-22 11:10:20 < ?xml version>
2017-05-22 11:10:20 < ?xml version>
2017-05-22 11:10:20 < ?xml version>
2017-05-22 11:10:20 < ?xml version>
2017-05-22 11:10:20 < ?xml version>
2017-05-22 12:10:20 < ?xml version>
2017-05-22 11:10:20 < ?xml version>
2017-05-22 12:10:20 < ?xml version>
sample text sample text
sample text sample text
sample text sample text
sample text sample text
sample text sample text
sample text sample text
sample text sample text
sample text sample text
sample text sample text
2017-05-22 11:10:20 < ?xml version>
2017-05-22 11:10:20 < ?xml version>
2017-05-22 11:10:20 < ?xml version>
2017-05-22 11:10:20 < ?xml version>
2017-05-22 11:10:20 < ?xml version>
2017-05-22 12:10:20 < ?xml version>
2017-05-22 11:10:20 < ?xml version>
2017-05-22 12:10:20 < ?xml version>
sample text sample text
sample text sample text
sample text sample text
sample text sample text
sample text sample text
sample text sample text
sample text sample text
sample text sample text
sample text sample text
2017-05-22 12:10:20 < ?xml version>
sample text sample text
2017-05-22 12:10:20 < ?xml version>
假设时间是12:30,那么输出就像 2017-05-22 12:10:20&lt; ?xml版本&gt; 2017-05-22 12:10:20&lt; ?xml版本&gt; 示例文本示例文本 示例文本示例文本 示例文本示例文本 示例文本示例文本 示例文本示例文本 示例文本示例文本 示例文本示例文本 示例文本示例文本 示例文本示例文本 2017-05-22 12:10:20&lt; ?xml版本&gt; 2017-05-22 12:10:20&lt; ?xml版本&gt; 示例文本示例文本 2017-05-22 12:10:20&lt; ?xml版本&gt;
我曾尝试过一些选项,但它给了我所有日志: - (
grep "^$(date -d -1hour +'%Y-%m-%d %H')" text.log
sed -e "1,/^$(date -d -1hour +'%Y-%m-%d %H')/d" text.log
在触发这些查询时,它会给我所有日志而不是一小时。 请给我一些建议?
答案 0 :(得分:0)
试一试:
grep "$(date -v-60M +"%Y-%m-%d %H")" text.log