如何在 /var/log/dpkg.log 中搜索每月安装/升级日志并输出到文本文件

How to search /var/log/dpkg.log for monthly install/upgrade logs and output to text file

提问人:Darren 提问时间:1/18/2021 最后编辑:Rishon_JRDarren 更新时间:12/10/2022 访问量:485

问:

我想通过查看来输出一个文件,其中包含仅限上个月的安装/升级日志。/var/log/dpkg.log

我不知道bash,我知道命令可以做到,但我仍然需要一些帮助。grep

Linux bash

评论

2赞 glenn jackman 1/18/2021
你会想阅读 我如何提出一个好问题?

答:

0赞 Synthase 1/18/2021 #1
$ grep "install\|upgrade" /var/log/dpkg.log | grep "2021-01" > log.dat

示例输出:

2021-01-11 23:05:50 status installed mime-support:all 3.62
2021-01-11 23:05:50 status installed hicolor-icon-theme:all 0.17-2
2021-01-11 23:05:53 status installed shared-mime-info:amd64 1.10-1
2021-01-12 11:11:57 install python3-configobj:all <none> 5.0.6-3
2021-01-12 11:11:57 status half-installed python3-configobj:all 5.0.6-3
2021-01-12 11:11:57 install python3-psutil:amd64 <none> 5.5.1-1
2021-01-12 11:11:57 status half-installed python3-psutil:amd64 5.5.1-1

评论

0赞 Darren 1/19/2021
谢谢 - 这正是我想要的。
0赞 Synthase 1/19/2021
@Darren 好!考虑接受答案,如果这对:)有帮助(灰色勾号我的答案的左上角)
0赞 Raman Sailopal 1/18/2021 #2

Awk 是实现此目的的理想解决方案(GNU awk 利用 gensub 函数)

 awk -v tstamp="$(date -d "-30 days" +%s)" '/install/ || /upgrade/ {dconv=gensub("-"," ","g",$1);tconv=gensub(":"," ","g",$2);dstamp=mktime(dconv" "tconv);if (dstamp >= tstamp ) { print } }' /var/log/dpkg.log

解释:

awk -v tstamp="$(date -d "-30 days" +%s)" ' # Pass the date minus 30 days as variable to awk (tstamp)
    /install/ || /upgrade/ {                                  # Process where lines 
                                                                contain install or 
                                                                upgrade
                             dconv=gensub("-"," ","g",$1);    # Replace "-" for " " in 
                                                                the first space 
                                                                delimited field (date) 
                                                                and read result into 
                                                                dcon
                             tconv=gensub(":"," ","g",$2);    # Replace ":" for " " in 
                                                                the second space 
                                                                delimited field (time) 
                                                                and read the result 
                                                                into tconv
                             dstamp=mktime(dconv" "tconv);    # Create epoch format of 
                                                                date and time
                             if (dstamp >= tstamp ) {         # See whether date/time 
                                                                is within 30 days by 
                                                                comparing dstamp to 
                                                                tstamp
                                 print                        # Print line if 
                                                                condition is met
                             } 
                            }' /var/log/dpkg.log