得到不。为日期范围打印的行数

时间:2017-01-02 13:42:18

标签: linux bash date awk sed

我正在努力得到不计数。在给定日期范围内打印的行数。

这是我的输入和输出:

grep -inr "\[HSM \]Handle Identity Request. Send Identity Response. timeout: 1550s" *ActNac*
ActNacd.txt:47:2017-01-02 09:10:13 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-20_20-39-18.txt:171:2016-12-19 13:23:48 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-20_20-39-18.txt:217:2016-12-19 14:27:26 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-26_09-44-47.txt:47:2016-12-25 19:26:13 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-26_09-44-47.txt:91:2016-12-25 20:30:38 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-27_07-32-25.txt:47:2016-12-26 11:08:28 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-27_18-06-29.txt:47:2016-12-27 07:35:55 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-30_08-22-16.txt:48:2016-12-28 18:04:19 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2017-01-02_06-56-52.txt:47:2016-12-30 08:24:19 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2017-01-02_09-07-23.txt:47:2017-01-02 06:59:33 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s

我想找到不。从2017-12-20至2017-12-30期间打印的行。

未确认开始日期和结束日期,因此如果我使用像

这样的sed命令
grep -inr "\[HSM \]Handle Identity Request. Send Identity Response. timeout: 1550s" *ActNac* | sed -n '/2016-12-20/,/2017-12-27/p'"

但如果我提供的输入如下:

grep -inr "\[HSM \]Handle Identity Request. Send Identity Response. timeout: 1550s" *ActNac* | sed -n '/2016-12-19/,/2017-12-27/p'"

它不提供任何输出,因为开始日期与任何行都不匹配作为输入日期。没有日期为2016-12-19的日志文件。

在一系列输入日期中,使用sed / awk / grep命令是否有其他方法可以获取行数。

3 个答案:

答案 0 :(得分:2)

您可以单独使用awk

单独计算,

awk -F'[: ]' '$3 >= "2016-12-16" && $3 <= "2016-12-26 && NR>1"{count++;}END{print count}' newfile
5

要获取实际日志,我使用了日志中实际不存在的日期,

awk -F'[: ]' '$3 >= "2016-12-22" && $3 <= "2016-12-29" && NR>1' newfile
old_ActNacd_2016-12-26_09-44-47.txt:47:2016-12-25 19:26:13 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-26_09-44-47.txt:91:2016-12-25 20:30:38 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-27_07-32-25.txt:47:2016-12-26 11:08:28 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-27_18-06-29.txt:47:2016-12-27 07:35:55 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s
old_ActNacd_2016-12-30_08-22-16.txt:48:2016-12-28 18:04:19 - [HSM ]Handle Identity Request. Send Identity Response. timeout: 1550s

要获取计数,请使用第一个命令中的{count++;}END{print count}子句。

答案 1 :(得分:0)

你遇到的困难与计算线路无关(这很简单,只需将数据传输到wc -l),但比较日期。如果您想比较日期,最简单的方法是将它们转换为合理的表示,使比较变得容易。例如,纪元时间。使用perl执行此操作的简单方法:

perl -mTime::Piece -ne 'm/[^:]*:[^:]*:([-0-9]*) /; 
    $t=Time::Piece->strptime($1, "%Y-%m-%d"); print if $t > 1482192000;' input

这将打印日期超过1482192000(格林威治标准时间12月20日午夜)的行。要计算它们,要么用perl计算它们:

perl -mTime::Piece -nE 'm/[^:]*:[^:]*:([-0-9]*) /; 
    $t=Time::Piece->strptime($1, "%Y-%m-%d"); $v++ if $t > 1482192000;
    END{say $v}' input

或只是管道到wc:

perl -mTime::Piece -ne 'm/[^:]*:[^:]*:([-0-9]*) /; 
    $t=Time::Piece->strptime($1, "%Y-%m-%d"); print if $t > 1482192000;' input | 
wc -l

答案 2 :(得分:0)

当所有行都有日期字符串时,您可以执行类似的操作 grep -Ec "^2016-12-2" *ActNac*(直到2016-12-30,不包括30日),

grep -Ec "^2016-12-(2|30)" *ActNac*