Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何仅从日志文件中grep最后30天的输入?_Regex_Linux_Bash_Ubuntu_Grep - Fatal编程技术网

Regex 如何仅从日志文件中grep最后30天的输入?

Regex 如何仅从日志文件中grep最后30天的输入?,regex,linux,bash,ubuntu,grep,Regex,Linux,Bash,Ubuntu,Grep,我想看看过去30天内升级的所有软件包 所以我运行这个命令: cat /var/log/apt/history.log | grep -a -A 2 Upgrade 我得到这个输出: ... Upgrade: gcc-8-base:amd64 (8.3.0-6ubuntu1~18.04.1, 8.3.0-26ubuntu1~18.04), libgcc1:amd64 (1:8.3.0-6ubuntu1~18.04.1, 1:8.3.0-26ubuntu1~18.04), libstdc++6:

我想看看过去30天内升级的所有软件包

所以我运行这个命令:

cat /var/log/apt/history.log | grep -a -A 2 Upgrade
我得到这个输出:

...

Upgrade: gcc-8-base:amd64 (8.3.0-6ubuntu1~18.04.1, 8.3.0-26ubuntu1~18.04), libgcc1:amd64 (1:8.3.0-6ubuntu1~18.04.1, 1:8.3.0-26ubuntu1~18.04), libstdc++6:amd64 (8.3.0-6ubuntu1~18.04.1, 8.3.0-26ubuntu1~18.04)
End-Date: 2020-03-24  10:01:31

--
Upgrade: libsasl2-2:amd64 (2.1.27~101-g0780600+dfsg-3ubuntu2, 2.1.27~101-g0780600+dfsg-3ubuntu2.1)
End-Date: 2020-03-24  10:01:35

--
Upgrade: libsystemd0:amd64 (237-3ubuntu10.33, 237-3ubuntu10.38), libpam-systemd:amd64 (237-3ubuntu10.33, 237-3ubuntu10.38), systemd:amd64 (237-3ubuntu10.33, 237-3ubuntu10.38), libnss-systemd:amd64 (237-3ubuntu10.33, 237-3ubuntu10.38)
End-Date: 2020-03-24  10:01:43

--
Upgrade: vim-common:amd64 (2:8.0.1453-1ubuntu1.1, 2:8.0.1453-1ubuntu1.3), vim-runtime:amd64 (2:8.0.1453-1ubuntu1.1, 2:8.0.1453-1ubuntu1.3), vim:amd64 (2:8.0.1453-1ubuntu1.1, 2:8.0.1453-1ubuntu1.3), vim-tiny:amd64 (2:8.0.1453-1ubuntu1.1, 2:8.0.1453-1ubuntu1.3)
End-Date: 2020-03-24  10:01:49

--
Upgrade: linux-headers-generic:amd64 (4.15.0.74.76, 4.15.0.91.83), linux-image-generic:amd64 (4.15.0.74.76, 4.15.0.91.83), linux-generic:amd64 (4.15.0.74.76, 4.15.0.91.83)
End-Date: 2020-03-24  10:02:29

--
Upgrade: git:amd64 (1:2.17.1-1ubuntu0.5, 1:2.17.1-1ubuntu0.7)
End-Date: 2020-04-27  08:31:00

--
Upgrade: git-man:amd64 (1:2.17.1-1ubuntu0.5, 1:2.17.1-1ubuntu0.7)
End-Date: 2020-04-27  08:31:06

--
Upgrade: python3-apport:amd64 (2.20.9-0ubuntu7.12, 2.20.9-0ubuntu7.14)
End-Date: 2020-04-27  08:31:09

--
Upgrade: python3-problem-report:amd64 (2.20.9-0ubuntu7.12, 2.20.9-0ubuntu7.14)
End-Date: 2020-04-27  08:31:12

--
Upgrade: apport:amd64 (2.20.9-0ubuntu7.12, 2.20.9-0ubuntu7.14)
End-Date: 2020-04-27  08:31:20


...

现在我只想grep过去30天中附加到文件中的行。我希望它总是与我当前的时间相关,所以我不必每次运行命令时都更改语法和日期,所以总是30天。正如您所见,行中有日期,因此应该可以这样做,但语法对我来说太复杂了。

您可以执行以下操作:

$ awk -v d="$(date -d "today -30 days" "+%F %T")"   \
      '(t && $2" "$3 > d){print t;print $0;t=""}
       /Upgrade/{t=$0}' log-file

对于
升级:
结束日期:
(有时会发生)之间有额外一行的情况,我将对kvantour的回答进行如下调整:


依我看,
grep
是错误的工具。只要你还不在你感兴趣的时间窗口中,我就简单地循环文件并忽略行。grep-a2也有一个模糊性,因为有时你有一行以
Remove:
开头,在
Upgrade:
之后,在
End Date:
之前。你想让你的脚本也把那行变灰吗?第二个问题。在我的系统中,上个月记录的行不再存档在
/var/log/apt/history.log
中,而是存档在压缩文件中,如
/var/log/apt/history.log.1.gz
/var/log/apt/history.log.2.gz
。。。其中最近的一个是
/var/log/apt/history.log.1.gz
。因此,grep
/var/log/apt/history.log
/var/log/apt/history.log.1.gz就足够了。我不知道,我想要升级行及其升级日期,但不知道语法来获得100%的时间。是的,这非常有效!谢谢现在我不知道应该给哪个用户打绿色的复选标记。还有,这是一条怎样的单行线,这样我就可以把它放在crontab中了?给他绿色的复选标记,然后向上投票我的解决方案。这个命令可以放在一行中:
awk-vd=“$(date-d“今日-30天”+%F%T”)”/^结束日期:/{if(T&&$2“$3>d){print T T;print$0;T=“”}/^升级:/{T=$0}'/var/log/apt/history.log
,但这和不可读一样难看。因此,我会将上面的四行复制到一个bash脚本中,然后从crontab调用该脚本。
awk -v d="$(date -d "today -30 days" "+%F %T")" '
  /^End-Date: /{if (t && $2" "$3 > d) {print t; print $0; t=""}}
  /^Upgrade: / {t=$0}
' /var/log/apt/history.log