AWK在具有自定义日期格式的文件中查找日期范围内的行 [英] Awk to find lines within date range in a file with custom date format
问题描述
我正在尝试查找文件中日期范围之间的所有行.但是,日期以非标准方式格式化. awk有没有办法阅读这些内容?日志文件的格式如下:
I'm trying to find all lines between a date range in a file. However dates are formatted in a non standard way. Is there a way for awk to read these? The log file is formatted like so:
Jan 5 11:34:00 log messages here
Jan 13 16:21:00 log messages here
Feb 1 01:14:00 log messages here
Feb 10 16:32:00 more messages
Mar 7 16:32:00 more messages
Apr 21 16:32:00 more messages
例如,如果我要拉1月1日至2月10日之间的所有行: 我尝试过:
For example if I want to pull all lines between January 1st and Feb 10th: I've tried:
awk 'BEGIN{IGNORECASE=1} ($0>=from&&$0<=to)' from="Jan 1 00:00:00" to="Feb 10 23:59:59"
这是一个只能访问awk的系统,所以我有点受限制.任何帮助将不胜感激.
It's a system that only has access to awk so I am kind of limited. Any help would be greatly appreciated.
到目前为止,非常感谢您提供的答案!他们的工作很棒,并帮助了我对AWK的理解.但是,我确实忘记提及我也需要包括时间.
Thanks alot for the answers so far! They've worked great and have helped my understanding of AWK. However I did forget to mention I need to be able to include the time as well.
例如,查找以下范围内的行:
For example finding lines in the range including and between:
Jan 1 12:34:00
和
Feb 20 14:23:01
基于@Cyrus提供的答案,我决定也使用它来分析时间:
Based on the answer provided by @Cyrus, I decided to use this to parse through times as well:
awk -v start="0101 10:23:22" -v stop="0210 14:21:02" \
'BEGIN{m["Jan"]="01"; m["Feb"]="02"; m["Mar"]="03"; m["Apr"]="04"}
{original = $0; $1 = m[$1]; $2 = sprintf("%.2d", $2)}
$1$2$3 >= start && $1$2$3 <= stop {print original}' file
awk -v start="0101 10:23:22" -v stop="0210 14:21:02" \
'BEGIN{m["Jan"]="01"; m["Feb"]="02"; m["Mar"]="03"; m["Apr"]="04"}
{original = $0; $1 = m[$1]; $2 = sprintf("%.2d", $2)}
$1$2$3 >= start && $1$2$3 <= stop {print original}' file
推荐答案
使用awk. 0101
是1月1日,而0210
是2月10日.
With awk. 0101
is January 1st and 0210
February 10th.
awk -v start="0101" -v stop="0210" \
'BEGIN{m["Jan"]="01"; m["Feb"]="02"; m["Mar"]="03"; m["Apr"]="04"}
{original = $0; $1 = m[$1]; $2 = sprintf("%.2d", $2)}
$1$2 >= start && $1$2 <= stop {print original}' file
输出:
Jan 5 11:34:00 log messages here
Jan 13 16:21:00 log messages here
Feb 1 01:14:00 log messages here
Feb 10 16:32:00 more messages
这篇关于AWK在具有自定义日期格式的文件中查找日期范围内的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!